gvisor.dev/gvisor@v0.0.0-20240520182842-f9d4d51c7e0f/pkg/tcpip/network/ipv4/icmp.go (about)

     1  // Copyright 2021 The gVisor Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package ipv4
    16  
    17  import (
    18  	"fmt"
    19  
    20  	"gvisor.dev/gvisor/pkg/buffer"
    21  	"gvisor.dev/gvisor/pkg/tcpip"
    22  	"gvisor.dev/gvisor/pkg/tcpip/checksum"
    23  	"gvisor.dev/gvisor/pkg/tcpip/header"
    24  	"gvisor.dev/gvisor/pkg/tcpip/header/parse"
    25  	"gvisor.dev/gvisor/pkg/tcpip/stack"
    26  )
    27  
    28  // icmpv4DestinationUnreachableSockError is a general ICMPv4 Destination
    29  // Unreachable error.
    30  //
    31  // +stateify savable
    32  type icmpv4DestinationUnreachableSockError struct{}
    33  
    34  // Origin implements tcpip.SockErrorCause.
    35  func (*icmpv4DestinationUnreachableSockError) Origin() tcpip.SockErrOrigin {
    36  	return tcpip.SockExtErrorOriginICMP
    37  }
    38  
    39  // Type implements tcpip.SockErrorCause.
    40  func (*icmpv4DestinationUnreachableSockError) Type() uint8 {
    41  	return uint8(header.ICMPv4DstUnreachable)
    42  }
    43  
    44  // Info implements tcpip.SockErrorCause.
    45  func (*icmpv4DestinationUnreachableSockError) Info() uint32 {
    46  	return 0
    47  }
    48  
    49  var _ stack.TransportError = (*icmpv4DestinationHostUnreachableSockError)(nil)
    50  
    51  // icmpv4DestinationHostUnreachableSockError is an ICMPv4 Destination Host
    52  // Unreachable error.
    53  //
    54  // It indicates that a packet was not able to reach the destination host.
    55  //
    56  // +stateify savable
    57  type icmpv4DestinationHostUnreachableSockError struct {
    58  	icmpv4DestinationUnreachableSockError
    59  }
    60  
    61  // Code implements tcpip.SockErrorCause.
    62  func (*icmpv4DestinationHostUnreachableSockError) Code() uint8 {
    63  	return uint8(header.ICMPv4HostUnreachable)
    64  }
    65  
    66  // Kind implements stack.TransportError.
    67  func (*icmpv4DestinationHostUnreachableSockError) Kind() stack.TransportErrorKind {
    68  	return stack.DestinationHostUnreachableTransportError
    69  }
    70  
    71  var _ stack.TransportError = (*icmpv4DestinationNetUnreachableSockError)(nil)
    72  
    73  // icmpv4DestinationNetUnreachableSockError is an ICMPv4 Destination Net
    74  // Unreachable error.
    75  //
    76  // It indicates that a packet was not able to reach the destination network.
    77  //
    78  // +stateify savable
    79  type icmpv4DestinationNetUnreachableSockError struct {
    80  	icmpv4DestinationUnreachableSockError
    81  }
    82  
    83  // Code implements tcpip.SockErrorCause.
    84  func (*icmpv4DestinationNetUnreachableSockError) Code() uint8 {
    85  	return uint8(header.ICMPv4NetUnreachable)
    86  }
    87  
    88  // Kind implements stack.TransportError.
    89  func (*icmpv4DestinationNetUnreachableSockError) Kind() stack.TransportErrorKind {
    90  	return stack.DestinationNetworkUnreachableTransportError
    91  }
    92  
    93  var _ stack.TransportError = (*icmpv4DestinationPortUnreachableSockError)(nil)
    94  
    95  // icmpv4DestinationPortUnreachableSockError is an ICMPv4 Destination Port
    96  // Unreachable error.
    97  //
    98  // It indicates that a packet reached the destination host, but the transport
    99  // protocol was not active on the destination port.
   100  //
   101  // +stateify savable
   102  type icmpv4DestinationPortUnreachableSockError struct {
   103  	icmpv4DestinationUnreachableSockError
   104  }
   105  
   106  // Code implements tcpip.SockErrorCause.
   107  func (*icmpv4DestinationPortUnreachableSockError) Code() uint8 {
   108  	return uint8(header.ICMPv4PortUnreachable)
   109  }
   110  
   111  // Kind implements stack.TransportError.
   112  func (*icmpv4DestinationPortUnreachableSockError) Kind() stack.TransportErrorKind {
   113  	return stack.DestinationPortUnreachableTransportError
   114  }
   115  
   116  var _ stack.TransportError = (*icmpv4DestinationProtoUnreachableSockError)(nil)
   117  
   118  // icmpv4DestinationProtoUnreachableSockError is an ICMPv4 Destination Protocol
   119  // Unreachable error.
   120  //
   121  // It indicates that a packet reached the destination host, but the transport
   122  // protocol was not reachable
   123  //
   124  // +stateify savable
   125  type icmpv4DestinationProtoUnreachableSockError struct {
   126  	icmpv4DestinationUnreachableSockError
   127  }
   128  
   129  // Code implements tcpip.SockErrorCause.
   130  func (*icmpv4DestinationProtoUnreachableSockError) Code() uint8 {
   131  	return uint8(header.ICMPv4ProtoUnreachable)
   132  }
   133  
   134  // Kind implements stack.TransportError.
   135  func (*icmpv4DestinationProtoUnreachableSockError) Kind() stack.TransportErrorKind {
   136  	return stack.DestinationProtoUnreachableTransportError
   137  }
   138  
   139  var _ stack.TransportError = (*icmpv4SourceRouteFailedSockError)(nil)
   140  
   141  // icmpv4SourceRouteFailedSockError is an ICMPv4 Destination Unreachable error
   142  // due to source route failed.
   143  //
   144  // +stateify savable
   145  type icmpv4SourceRouteFailedSockError struct {
   146  	icmpv4DestinationUnreachableSockError
   147  }
   148  
   149  // Code implements tcpip.SockErrorCause.
   150  func (*icmpv4SourceRouteFailedSockError) Code() uint8 {
   151  	return uint8(header.ICMPv4SourceRouteFailed)
   152  }
   153  
   154  // Kind implements stack.TransportError.
   155  func (*icmpv4SourceRouteFailedSockError) Kind() stack.TransportErrorKind {
   156  	return stack.SourceRouteFailedTransportError
   157  }
   158  
   159  var _ stack.TransportError = (*icmpv4SourceHostIsolatedSockError)(nil)
   160  
   161  // icmpv4SourceHostIsolatedSockError is an ICMPv4 Destination Unreachable error
   162  // due to source host isolated (not on the network).
   163  //
   164  // +stateify savable
   165  type icmpv4SourceHostIsolatedSockError struct {
   166  	icmpv4DestinationUnreachableSockError
   167  }
   168  
   169  // Code implements tcpip.SockErrorCause.
   170  func (*icmpv4SourceHostIsolatedSockError) Code() uint8 {
   171  	return uint8(header.ICMPv4SourceHostIsolated)
   172  }
   173  
   174  // Kind implements stack.TransportError.
   175  func (*icmpv4SourceHostIsolatedSockError) Kind() stack.TransportErrorKind {
   176  	return stack.SourceHostIsolatedTransportError
   177  }
   178  
   179  var _ stack.TransportError = (*icmpv4DestinationHostUnknownSockError)(nil)
   180  
   181  // icmpv4DestinationHostUnknownSockError is an ICMPv4 Destination Unreachable
   182  // error due to destination host unknown/down.
   183  //
   184  // +stateify savable
   185  type icmpv4DestinationHostUnknownSockError struct {
   186  	icmpv4DestinationUnreachableSockError
   187  }
   188  
   189  // Code implements tcpip.SockErrorCause.
   190  func (*icmpv4DestinationHostUnknownSockError) Code() uint8 {
   191  	return uint8(header.ICMPv4DestinationHostUnknown)
   192  }
   193  
   194  // Kind implements stack.TransportError.
   195  func (*icmpv4DestinationHostUnknownSockError) Kind() stack.TransportErrorKind {
   196  	return stack.DestinationHostDownTransportError
   197  }
   198  
   199  var _ stack.TransportError = (*icmpv4FragmentationNeededSockError)(nil)
   200  
   201  // icmpv4FragmentationNeededSockError is an ICMPv4 Destination Unreachable error
   202  // due to fragmentation being required but the packet was set to not be
   203  // fragmented.
   204  //
   205  // It indicates that a link exists on the path to the destination with an MTU
   206  // that is too small to carry the packet.
   207  //
   208  // +stateify savable
   209  type icmpv4FragmentationNeededSockError struct {
   210  	icmpv4DestinationUnreachableSockError
   211  
   212  	mtu uint32
   213  }
   214  
   215  // Code implements tcpip.SockErrorCause.
   216  func (*icmpv4FragmentationNeededSockError) Code() uint8 {
   217  	return uint8(header.ICMPv4FragmentationNeeded)
   218  }
   219  
   220  // Info implements tcpip.SockErrorCause.
   221  func (e *icmpv4FragmentationNeededSockError) Info() uint32 {
   222  	return e.mtu
   223  }
   224  
   225  // Kind implements stack.TransportError.
   226  func (*icmpv4FragmentationNeededSockError) Kind() stack.TransportErrorKind {
   227  	return stack.PacketTooBigTransportError
   228  }
   229  
   230  func (e *endpoint) checkLocalAddress(addr tcpip.Address) bool {
   231  	if e.nic.Spoofing() {
   232  		return true
   233  	}
   234  
   235  	if addressEndpoint := e.AcquireAssignedAddress(addr, false, stack.NeverPrimaryEndpoint, true /* readOnly */); addressEndpoint != nil {
   236  		return true
   237  	}
   238  	return false
   239  }
   240  
   241  // handleControl handles the case when an ICMP error packet contains the headers
   242  // of the original packet that caused the ICMP one to be sent. This information
   243  // is used to find out which transport endpoint must be notified about the ICMP
   244  // packet. We only expect the payload, not the enclosing ICMP packet.
   245  func (e *endpoint) handleControl(errInfo stack.TransportError, pkt *stack.PacketBuffer) {
   246  	h, ok := pkt.Data().PullUp(header.IPv4MinimumSize)
   247  	if !ok {
   248  		return
   249  	}
   250  	hdr := header.IPv4(h)
   251  
   252  	// We don't use IsValid() here because ICMP only requires that the IP
   253  	// header plus 8 bytes of the transport header be included. So it's
   254  	// likely that it is truncated, which would cause IsValid to return
   255  	// false.
   256  	//
   257  	// Drop packet if it doesn't have the basic IPv4 header or if the
   258  	// original source address doesn't match an address we own.
   259  	srcAddr := hdr.SourceAddress()
   260  	if !e.checkLocalAddress(srcAddr) {
   261  		return
   262  	}
   263  
   264  	hlen := int(hdr.HeaderLength())
   265  	if pkt.Data().Size() < hlen || hdr.FragmentOffset() != 0 {
   266  		// We won't be able to handle this if it doesn't contain the
   267  		// full IPv4 header, or if it's a fragment not at offset 0
   268  		// (because it won't have the transport header).
   269  		return
   270  	}
   271  
   272  	// Keep needed information before trimming header.
   273  	p := hdr.TransportProtocol()
   274  	dstAddr := hdr.DestinationAddress()
   275  	// Skip the ip header, then deliver the error.
   276  	if _, ok := pkt.Data().Consume(hlen); !ok {
   277  		panic(fmt.Sprintf("could not consume the IP header of %d bytes", hlen))
   278  	}
   279  	e.dispatcher.DeliverTransportError(srcAddr, dstAddr, ProtocolNumber, p, errInfo, pkt)
   280  }
   281  
   282  func (e *endpoint) handleICMP(pkt *stack.PacketBuffer) {
   283  	received := e.stats.icmp.packetsReceived
   284  	h := header.ICMPv4(pkt.TransportHeader().Slice())
   285  	if len(h) < header.ICMPv4MinimumSize {
   286  		received.invalid.Increment()
   287  		return
   288  	}
   289  
   290  	// Only do in-stack processing if the checksum is correct.
   291  	if checksum.Checksum(h, pkt.Data().Checksum()) != 0xffff {
   292  		received.invalid.Increment()
   293  		// It's possible that a raw socket expects to receive this regardless
   294  		// of checksum errors. If it's an echo request we know it's safe because
   295  		// we are the only handler, however other types do not cope well with
   296  		// packets with checksum errors.
   297  		switch h.Type() {
   298  		case header.ICMPv4Echo:
   299  			e.dispatcher.DeliverTransportPacket(header.ICMPv4ProtocolNumber, pkt)
   300  		}
   301  		return
   302  	}
   303  
   304  	iph := header.IPv4(pkt.NetworkHeader().Slice())
   305  	var newOptions header.IPv4Options
   306  	if opts := iph.Options(); len(opts) != 0 {
   307  		// RFC 1122 section 3.2.2.6 (page 43) (and similar for other round trip
   308  		// type ICMP packets):
   309  		//    If a Record Route and/or Time Stamp option is received in an
   310  		//    ICMP Echo Request, this option (these options) SHOULD be
   311  		//    updated to include the current host and included in the IP
   312  		//    header of the Echo Reply message, without "truncation".
   313  		//    Thus, the recorded route will be for the entire round trip.
   314  		//
   315  		// So we need to let the option processor know how it should handle them.
   316  		var op optionsUsage
   317  		if h.Type() == header.ICMPv4Echo {
   318  			op = &optionUsageEcho{}
   319  		} else {
   320  			op = &optionUsageReceive{}
   321  		}
   322  		var optProblem *header.IPv4OptParameterProblem
   323  		newOptions, _, optProblem = e.processIPOptions(pkt, opts, op)
   324  		if optProblem != nil {
   325  			if optProblem.NeedICMP {
   326  				_ = e.protocol.returnError(&icmpReasonParamProblem{
   327  					pointer: optProblem.Pointer,
   328  				}, pkt, true /* deliveredLocally */)
   329  				e.stats.ip.MalformedPacketsReceived.Increment()
   330  			}
   331  			return
   332  		}
   333  		copied := copy(opts, newOptions)
   334  		if copied != len(newOptions) {
   335  			panic(fmt.Sprintf("copied %d bytes of new options, expected %d bytes", copied, len(newOptions)))
   336  		}
   337  		for i := copied; i < len(opts); i++ {
   338  			// Pad with 0 (EOL). RFC 791 page 23 says "The padding is zero".
   339  			opts[i] = byte(header.IPv4OptionListEndType)
   340  		}
   341  	}
   342  
   343  	// TODO(b/112892170): Meaningfully handle all ICMP types.
   344  	switch h.Type() {
   345  	case header.ICMPv4Echo:
   346  		received.echoRequest.Increment()
   347  
   348  		// DeliverTransportPacket may modify pkt so don't use it beyond
   349  		// this point. Make a deep copy of the data before pkt gets sent as we will
   350  		// be modifying fields. Both the ICMP header (with its type modified to
   351  		// EchoReply) and payload are reused in the reply packet.
   352  		//
   353  		// TODO(gvisor.dev/issue/4399): The copy may not be needed if there are no
   354  		// waiting endpoints. Consider moving responsibility for doing the copy to
   355  		// DeliverTransportPacket so that is is only done when needed.
   356  		replyData := stack.PayloadSince(pkt.TransportHeader())
   357  		defer replyData.Release()
   358  		ipHdr := header.IPv4(pkt.NetworkHeader().Slice())
   359  		localAddressBroadcast := pkt.NetworkPacketInfo.LocalAddressBroadcast
   360  
   361  		// It's possible that a raw socket expects to receive this.
   362  		e.dispatcher.DeliverTransportPacket(header.ICMPv4ProtocolNumber, pkt)
   363  		pkt = nil
   364  
   365  		sent := e.stats.icmp.packetsSent
   366  		if !e.protocol.allowICMPReply(header.ICMPv4EchoReply, header.ICMPv4UnusedCode) {
   367  			sent.rateLimited.Increment()
   368  			return
   369  		}
   370  
   371  		// As per RFC 1122 section 3.2.1.3, when a host sends any datagram, the IP
   372  		// source address MUST be one of its own IP addresses (but not a broadcast
   373  		// or multicast address).
   374  		localAddr := ipHdr.DestinationAddress()
   375  		if localAddressBroadcast || header.IsV4MulticastAddress(localAddr) {
   376  			localAddr = tcpip.Address{}
   377  		}
   378  
   379  		r, err := e.protocol.stack.FindRoute(e.nic.ID(), localAddr, ipHdr.SourceAddress(), ProtocolNumber, false /* multicastLoop */)
   380  		if err != nil {
   381  			// If we cannot find a route to the destination, silently drop the packet.
   382  			return
   383  		}
   384  		defer r.Release()
   385  
   386  		outgoingEP, ok := e.protocol.getEndpointForNIC(r.NICID())
   387  		if !ok {
   388  			// The outgoing NIC went away.
   389  			sent.dropped.Increment()
   390  			return
   391  		}
   392  
   393  		// Because IP and ICMP are so closely intertwined, we need to handcraft our
   394  		// IP header to be able to follow RFC 792. The wording on page 13 is as
   395  		// follows:
   396  		//   IP Fields:
   397  		//   Addresses
   398  		//     The address of the source in an echo message will be the
   399  		//     destination of the echo reply message.  To form an echo reply
   400  		//     message, the source and destination addresses are simply reversed,
   401  		//     the type code changed to 0, and the checksum recomputed.
   402  		//
   403  		// This was interpreted by early implementors to mean that all options must
   404  		// be copied from the echo request IP header to the echo reply IP header
   405  		// and this behaviour is still relied upon by some applications.
   406  		//
   407  		// Create a copy of the IP header we received, options and all, and change
   408  		// The fields we need to alter.
   409  		//
   410  		// We need to produce the entire packet in the data segment in order to
   411  		// use WriteHeaderIncludedPacket(). WriteHeaderIncludedPacket sets the
   412  		// total length and the header checksum so we don't need to set those here.
   413  		//
   414  		// Take the base of the incoming request IP header but replace the options.
   415  		replyHeaderLength := uint8(header.IPv4MinimumSize + len(newOptions))
   416  		replyIPHdrView := buffer.NewView(int(replyHeaderLength))
   417  		replyIPHdrView.Write(iph[:header.IPv4MinimumSize])
   418  		replyIPHdrView.Write(newOptions)
   419  		replyIPHdr := header.IPv4(replyIPHdrView.AsSlice())
   420  		replyIPHdr.SetHeaderLength(replyHeaderLength)
   421  		replyIPHdr.SetSourceAddress(r.LocalAddress())
   422  		replyIPHdr.SetDestinationAddress(r.RemoteAddress())
   423  		replyIPHdr.SetTTL(r.DefaultTTL())
   424  		replyIPHdr.SetTotalLength(uint16(len(replyIPHdr) + len(replyData.AsSlice())))
   425  		replyIPHdr.SetChecksum(0)
   426  		replyIPHdr.SetChecksum(^replyIPHdr.CalculateChecksum())
   427  
   428  		replyICMPHdr := header.ICMPv4(replyData.AsSlice())
   429  		replyICMPHdr.SetType(header.ICMPv4EchoReply)
   430  		replyICMPHdr.SetChecksum(0)
   431  		replyICMPHdr.SetChecksum(^checksum.Checksum(replyData.AsSlice(), 0))
   432  
   433  		replyBuf := buffer.MakeWithView(replyIPHdrView)
   434  		replyBuf.Append(replyData.Clone())
   435  		replyPkt := stack.NewPacketBuffer(stack.PacketBufferOptions{
   436  			ReserveHeaderBytes: int(r.MaxHeaderLength()),
   437  			Payload:            replyBuf,
   438  		})
   439  		defer replyPkt.DecRef()
   440  		// Populate the network/transport headers in the packet buffer so the
   441  		// ICMP packet goes through IPTables.
   442  		if ok := parse.IPv4(replyPkt); !ok {
   443  			panic("expected to parse IPv4 header we just created")
   444  		}
   445  		if ok := parse.ICMPv4(replyPkt); !ok {
   446  			panic("expected to parse ICMPv4 header we just created")
   447  		}
   448  
   449  		if err := outgoingEP.writePacket(r, replyPkt); err != nil {
   450  			sent.dropped.Increment()
   451  			return
   452  		}
   453  		sent.echoReply.Increment()
   454  
   455  	case header.ICMPv4EchoReply:
   456  		received.echoReply.Increment()
   457  
   458  		// ICMP sockets expect the ICMP header to be present, so we don't consume
   459  		// the ICMP header.
   460  		e.dispatcher.DeliverTransportPacket(header.ICMPv4ProtocolNumber, pkt)
   461  
   462  	case header.ICMPv4DstUnreachable:
   463  		received.dstUnreachable.Increment()
   464  
   465  		mtu := h.MTU()
   466  		code := h.Code()
   467  		switch code {
   468  		case header.ICMPv4NetUnreachable,
   469  			header.ICMPv4DestinationNetworkUnknown,
   470  			header.ICMPv4NetUnreachableForTos,
   471  			header.ICMPv4NetProhibited:
   472  			e.handleControl(&icmpv4DestinationNetUnreachableSockError{}, pkt)
   473  		case header.ICMPv4HostUnreachable,
   474  			header.ICMPv4HostProhibited,
   475  			header.ICMPv4AdminProhibited,
   476  			header.ICMPv4HostUnreachableForTos,
   477  			header.ICMPv4HostPrecedenceViolation,
   478  			header.ICMPv4PrecedenceCutInEffect:
   479  			e.handleControl(&icmpv4DestinationHostUnreachableSockError{}, pkt)
   480  		case header.ICMPv4PortUnreachable:
   481  			e.handleControl(&icmpv4DestinationPortUnreachableSockError{}, pkt)
   482  		case header.ICMPv4FragmentationNeeded:
   483  			networkMTU, err := calculateNetworkMTU(uint32(mtu), header.IPv4MinimumSize)
   484  			if err != nil {
   485  				networkMTU = 0
   486  			}
   487  			e.handleControl(&icmpv4FragmentationNeededSockError{mtu: networkMTU}, pkt)
   488  		case header.ICMPv4ProtoUnreachable:
   489  			e.handleControl(&icmpv4DestinationProtoUnreachableSockError{}, pkt)
   490  		case header.ICMPv4SourceRouteFailed:
   491  			e.handleControl(&icmpv4SourceRouteFailedSockError{}, pkt)
   492  		case header.ICMPv4SourceHostIsolated:
   493  			e.handleControl(&icmpv4SourceHostIsolatedSockError{}, pkt)
   494  		case header.ICMPv4DestinationHostUnknown:
   495  			e.handleControl(&icmpv4DestinationHostUnknownSockError{}, pkt)
   496  		}
   497  	case header.ICMPv4SrcQuench:
   498  		received.srcQuench.Increment()
   499  
   500  	case header.ICMPv4Redirect:
   501  		received.redirect.Increment()
   502  
   503  	case header.ICMPv4TimeExceeded:
   504  		received.timeExceeded.Increment()
   505  
   506  	case header.ICMPv4ParamProblem:
   507  		received.paramProblem.Increment()
   508  
   509  	case header.ICMPv4Timestamp:
   510  		received.timestamp.Increment()
   511  
   512  	case header.ICMPv4TimestampReply:
   513  		received.timestampReply.Increment()
   514  
   515  	case header.ICMPv4InfoRequest:
   516  		received.infoRequest.Increment()
   517  
   518  	case header.ICMPv4InfoReply:
   519  		received.infoReply.Increment()
   520  
   521  	default:
   522  		received.invalid.Increment()
   523  	}
   524  }
   525  
   526  // ======= ICMP Error packet generation =========
   527  
   528  // icmpReason is a marker interface for IPv4 specific ICMP errors.
   529  type icmpReason interface {
   530  	isICMPReason()
   531  }
   532  
   533  // icmpReasonNetworkProhibited is an error where the destination network is
   534  // prohibited.
   535  type icmpReasonNetworkProhibited struct{}
   536  
   537  func (*icmpReasonNetworkProhibited) isICMPReason() {}
   538  
   539  // icmpReasonHostProhibited is an error where the destination host is
   540  // prohibited.
   541  type icmpReasonHostProhibited struct{}
   542  
   543  func (*icmpReasonHostProhibited) isICMPReason() {}
   544  
   545  // icmpReasonAdministrativelyProhibited is an error where the destination is
   546  // administratively prohibited.
   547  type icmpReasonAdministrativelyProhibited struct{}
   548  
   549  func (*icmpReasonAdministrativelyProhibited) isICMPReason() {}
   550  
   551  // icmpReasonPortUnreachable is an error where the transport protocol has no
   552  // listener and no alternative means to inform the sender.
   553  type icmpReasonPortUnreachable struct{}
   554  
   555  func (*icmpReasonPortUnreachable) isICMPReason() {}
   556  
   557  // icmpReasonProtoUnreachable is an error where the transport protocol is
   558  // not supported.
   559  type icmpReasonProtoUnreachable struct{}
   560  
   561  func (*icmpReasonProtoUnreachable) isICMPReason() {}
   562  
   563  // icmpReasonTTLExceeded is an error where a packet's time to live exceeded in
   564  // transit to its final destination, as per RFC 792 page 6, Time Exceeded
   565  // Message.
   566  type icmpReasonTTLExceeded struct{}
   567  
   568  func (*icmpReasonTTLExceeded) isICMPReason() {}
   569  
   570  // icmpReasonReassemblyTimeout is an error where insufficient fragments are
   571  // received to complete reassembly of a packet within a configured time after
   572  // the reception of the first-arriving fragment of that packet.
   573  type icmpReasonReassemblyTimeout struct{}
   574  
   575  func (*icmpReasonReassemblyTimeout) isICMPReason() {}
   576  
   577  // icmpReasonParamProblem is an error to use to request a Parameter Problem
   578  // message to be sent.
   579  type icmpReasonParamProblem struct {
   580  	pointer byte
   581  }
   582  
   583  func (*icmpReasonParamProblem) isICMPReason() {}
   584  
   585  // icmpReasonNetworkUnreachable is an error in which the network specified in
   586  // the internet destination field of the datagram is unreachable.
   587  type icmpReasonNetworkUnreachable struct{}
   588  
   589  func (*icmpReasonNetworkUnreachable) isICMPReason() {}
   590  
   591  // icmpReasonFragmentationNeeded is an error where a packet requires
   592  // fragmentation while also having the Don't Fragment flag set, as per RFC 792
   593  // page 3, Destination Unreachable Message.
   594  type icmpReasonFragmentationNeeded struct{}
   595  
   596  func (*icmpReasonFragmentationNeeded) isICMPReason() {}
   597  
   598  // icmpReasonHostUnreachable is an error in which the host specified in the
   599  // internet destination field of the datagram is unreachable.
   600  type icmpReasonHostUnreachable struct{}
   601  
   602  func (*icmpReasonHostUnreachable) isICMPReason() {}
   603  
   604  // returnError takes an error descriptor and generates the appropriate ICMP
   605  // error packet for IPv4 and sends it back to the remote device that sent
   606  // the problematic packet. It incorporates as much of that packet as
   607  // possible as well as any error metadata as is available. returnError
   608  // expects pkt to hold a valid IPv4 packet as per the wire format.
   609  func (p *protocol) returnError(reason icmpReason, pkt *stack.PacketBuffer, deliveredLocally bool) tcpip.Error {
   610  	origIPHdr := header.IPv4(pkt.NetworkHeader().Slice())
   611  	origIPHdrSrc := origIPHdr.SourceAddress()
   612  	origIPHdrDst := origIPHdr.DestinationAddress()
   613  
   614  	// We check we are responding only when we are allowed to.
   615  	// See RFC 1812 section 4.3.2.7 (shown below).
   616  	//
   617  	// =========
   618  	// 4.3.2.7 When Not to Send ICMP Errors
   619  	//
   620  	//  An ICMP error message MUST NOT be sent as the result of receiving:
   621  	//
   622  	//  o An ICMP error message, or
   623  	//
   624  	//  o A packet which fails the IP header validation tests described in
   625  	//    Section [5.2.2] (except where that section specifically permits
   626  	//    the sending of an ICMP error message), or
   627  	//
   628  	//  o A packet destined to an IP broadcast or IP multicast address, or
   629  	//
   630  	//  o A packet sent as a Link Layer broadcast or multicast, or
   631  	//
   632  	//  o Any fragment of a datagram other then the first fragment (i.e., a
   633  	// packet for which the fragment offset in the IP header is nonzero).
   634  	//
   635  	// TODO(gvisor.dev/issues/4058): Make sure we don't send ICMP errors in
   636  	// response to a non-initial fragment, but it currently can not happen.
   637  	if pkt.NetworkPacketInfo.LocalAddressBroadcast || header.IsV4MulticastAddress(origIPHdrDst) || origIPHdrSrc == header.IPv4Any {
   638  		return nil
   639  	}
   640  
   641  	// If the packet wasn't delivered locally, do not use the packet's destination
   642  	// address as the response's source address as we should not not own the
   643  	// destination address of a packet we are forwarding.
   644  	localAddr := origIPHdrDst
   645  	if !deliveredLocally {
   646  		localAddr = tcpip.Address{}
   647  	}
   648  
   649  	// Even if we were able to receive a packet from some remote, we may not have
   650  	// a route to it - the remote may be blocked via routing rules. We must always
   651  	// consult our routing table and find a route to the remote before sending any
   652  	// packet.
   653  	route, err := p.stack.FindRoute(pkt.NICID, localAddr, origIPHdrSrc, ProtocolNumber, false /* multicastLoop */)
   654  	if err != nil {
   655  		return err
   656  	}
   657  	defer route.Release()
   658  
   659  	p.mu.Lock()
   660  	// We retrieve an endpoint using the newly constructed route's NICID rather
   661  	// than the packet's NICID. The packet's NICID corresponds to the NIC on
   662  	// which it arrived, which isn't necessarily the same as the NIC on which it
   663  	// will be transmitted. On the other hand, the route's NIC *is* guaranteed
   664  	// to be the NIC on which the packet will be transmitted.
   665  	netEP, ok := p.eps[route.NICID()]
   666  	p.mu.Unlock()
   667  	if !ok {
   668  		return &tcpip.ErrNotConnected{}
   669  	}
   670  
   671  	transportHeader := pkt.TransportHeader().Slice()
   672  
   673  	// Don't respond to icmp error packets.
   674  	if origIPHdr.Protocol() == uint8(header.ICMPv4ProtocolNumber) {
   675  		// We need to decide to explicitly name the packets we can respond to or
   676  		// the ones we can not respond to. The decision is somewhat arbitrary and
   677  		// if problems arise this could be reversed. It was judged less of a breach
   678  		// of protocol to not respond to unknown non-error packets than to respond
   679  		// to unknown error packets so we take the first approach.
   680  		if len(transportHeader) < header.ICMPv4MinimumSize {
   681  			// The packet is malformed.
   682  			return nil
   683  		}
   684  		switch header.ICMPv4(transportHeader).Type() {
   685  		case
   686  			header.ICMPv4EchoReply,
   687  			header.ICMPv4Echo,
   688  			header.ICMPv4Timestamp,
   689  			header.ICMPv4TimestampReply,
   690  			header.ICMPv4InfoRequest,
   691  			header.ICMPv4InfoReply:
   692  		default:
   693  			// Assume any type we don't know about may be an error type.
   694  			return nil
   695  		}
   696  	}
   697  
   698  	sent := netEP.stats.icmp.packetsSent
   699  	icmpType, icmpCode, counter, pointer := func() (header.ICMPv4Type, header.ICMPv4Code, tcpip.MultiCounterStat, byte) {
   700  		switch reason := reason.(type) {
   701  		case *icmpReasonNetworkProhibited:
   702  			return header.ICMPv4DstUnreachable, header.ICMPv4NetProhibited, sent.dstUnreachable, 0
   703  		case *icmpReasonHostProhibited:
   704  			return header.ICMPv4DstUnreachable, header.ICMPv4HostProhibited, sent.dstUnreachable, 0
   705  		case *icmpReasonAdministrativelyProhibited:
   706  			return header.ICMPv4DstUnreachable, header.ICMPv4AdminProhibited, sent.dstUnreachable, 0
   707  		case *icmpReasonPortUnreachable:
   708  			return header.ICMPv4DstUnreachable, header.ICMPv4PortUnreachable, sent.dstUnreachable, 0
   709  		case *icmpReasonProtoUnreachable:
   710  			return header.ICMPv4DstUnreachable, header.ICMPv4ProtoUnreachable, sent.dstUnreachable, 0
   711  		case *icmpReasonNetworkUnreachable:
   712  			return header.ICMPv4DstUnreachable, header.ICMPv4NetUnreachable, sent.dstUnreachable, 0
   713  		case *icmpReasonHostUnreachable:
   714  			return header.ICMPv4DstUnreachable, header.ICMPv4HostUnreachable, sent.dstUnreachable, 0
   715  		case *icmpReasonFragmentationNeeded:
   716  			return header.ICMPv4DstUnreachable, header.ICMPv4FragmentationNeeded, sent.dstUnreachable, 0
   717  		case *icmpReasonTTLExceeded:
   718  			return header.ICMPv4TimeExceeded, header.ICMPv4TTLExceeded, sent.timeExceeded, 0
   719  		case *icmpReasonReassemblyTimeout:
   720  			return header.ICMPv4TimeExceeded, header.ICMPv4ReassemblyTimeout, sent.timeExceeded, 0
   721  		case *icmpReasonParamProblem:
   722  			return header.ICMPv4ParamProblem, header.ICMPv4UnusedCode, sent.paramProblem, reason.pointer
   723  		default:
   724  			panic(fmt.Sprintf("unsupported ICMP type %T", reason))
   725  		}
   726  	}()
   727  
   728  	if !p.allowICMPReply(icmpType, icmpCode) {
   729  		sent.rateLimited.Increment()
   730  		return nil
   731  	}
   732  
   733  	// Now work out how much of the triggering packet we should return.
   734  	// As per RFC 1812 Section 4.3.2.3
   735  	//
   736  	//   ICMP datagram SHOULD contain as much of the original
   737  	//   datagram as possible without the length of the ICMP
   738  	//   datagram exceeding 576 bytes.
   739  	//
   740  	// NOTE: The above RFC referenced is different from the original
   741  	// recommendation in RFC 1122 and RFC 792 where it mentioned that at
   742  	// least 8 bytes of the payload must be included. Today linux and other
   743  	// systems implement the RFC 1812 definition and not the original
   744  	// requirement. We treat 8 bytes as the minimum but will try send more.
   745  	mtu := int(route.MTU())
   746  	const maxIPData = header.IPv4MinimumProcessableDatagramSize - header.IPv4MinimumSize
   747  	if mtu > maxIPData {
   748  		mtu = maxIPData
   749  	}
   750  	available := mtu - header.ICMPv4MinimumSize
   751  
   752  	if available < len(origIPHdr)+header.ICMPv4MinimumErrorPayloadSize {
   753  		return nil
   754  	}
   755  
   756  	payloadLen := len(origIPHdr) + len(transportHeader) + pkt.Data().Size()
   757  	if payloadLen > available {
   758  		payloadLen = available
   759  	}
   760  
   761  	// The buffers used by pkt may be used elsewhere in the system.
   762  	// For example, an AF_RAW or AF_PACKET socket may use what the transport
   763  	// protocol considers an unreachable destination. Thus we deep copy pkt to
   764  	// prevent multiple ownership and SR errors. The new copy is a vectorized
   765  	// view with the entire incoming IP packet reassembled and truncated as
   766  	// required. This is now the payload of the new ICMP packet and no longer
   767  	// considered a packet in its own right.
   768  
   769  	payload := buffer.MakeWithView(pkt.NetworkHeader().View())
   770  	payload.Append(pkt.TransportHeader().View())
   771  	if dataCap := payloadLen - int(payload.Size()); dataCap > 0 {
   772  		buf := pkt.Data().ToBuffer()
   773  		buf.Truncate(int64(dataCap))
   774  		payload.Merge(&buf)
   775  	} else {
   776  		payload.Truncate(int64(payloadLen))
   777  	}
   778  
   779  	icmpPkt := stack.NewPacketBuffer(stack.PacketBufferOptions{
   780  		ReserveHeaderBytes: int(route.MaxHeaderLength()) + header.ICMPv4MinimumSize,
   781  		Payload:            payload,
   782  	})
   783  	defer icmpPkt.DecRef()
   784  
   785  	icmpPkt.TransportProtocolNumber = header.ICMPv4ProtocolNumber
   786  
   787  	icmpHdr := header.ICMPv4(icmpPkt.TransportHeader().Push(header.ICMPv4MinimumSize))
   788  	icmpHdr.SetCode(icmpCode)
   789  	icmpHdr.SetType(icmpType)
   790  	icmpHdr.SetPointer(pointer)
   791  	icmpHdr.SetChecksum(header.ICMPv4Checksum(icmpHdr, icmpPkt.Data().Checksum()))
   792  
   793  	if err := route.WritePacket(
   794  		stack.NetworkHeaderParams{
   795  			Protocol: header.ICMPv4ProtocolNumber,
   796  			TTL:      route.DefaultTTL(),
   797  			TOS:      stack.DefaultTOS,
   798  		},
   799  		icmpPkt,
   800  	); err != nil {
   801  		sent.dropped.Increment()
   802  		return err
   803  	}
   804  	counter.Increment()
   805  	return nil
   806  }
   807  
   808  // OnReassemblyTimeout implements fragmentation.TimeoutHandler.
   809  func (p *protocol) OnReassemblyTimeout(pkt *stack.PacketBuffer) {
   810  	// OnReassemblyTimeout sends a Time Exceeded Message, as per RFC 792:
   811  	//
   812  	//   If a host reassembling a fragmented datagram cannot complete the
   813  	//   reassembly due to missing fragments within its time limit it discards the
   814  	//   datagram, and it may send a time exceeded message.
   815  	//
   816  	//   If fragment zero is not available then no time exceeded need be sent at
   817  	//   all.
   818  	if pkt != nil {
   819  		p.returnError(&icmpReasonReassemblyTimeout{}, pkt, true /* deliveredLocally */)
   820  	}
   821  }