tcp-ip ICMP: Internet Control Message Protocol

来源:互联网 发布:iPhone软件源 编辑:程序博客网 时间:2024/04/29 22:26

ICMP is often considered part of the IP layer. It communicates error messages and other conditions that require attention.

ICMP Message Types

ICMP error messages are sometimes handled specially. For example, an ICMP error message is never generated in response to an ICMP error message. (If this were not the rule, we could end up with scenarios where an error generates an error, which generates an error, and so on, indefinitely).

When an ICMP error message is sent, the message always contains the IP header and the first 8 bytes of the IP datagram that caused the ICMP error to be generated. This lets the receiving ICMP module associate the message with one particular protocol (TCP or UDP from the protocol field in the IP header) and one particular user process (from the TCP or UDP port numbers that are in the TCP or UDP header contained in the first 8 bytes of the IP datagram).

An ICMP error message is never generated in response to

  1. An ICMP error message. (An ICMP error message may, however, be generated inresponse to an ICMP query message.)
  2. A datagram destined to an IP broadcast address or an IP multicast address (a class D address).
  3. A datagram sent as a link-layer broadcast.
  4. A fragment other than the first.
  5. A datagram whose source address does not define a single host. This means the source address cannot be a zero address, a loopback address, a broadcast address, or
    a multicast address.

ICMP Address Mask Request and Reply

The ICMP address mask request is intended for a diskless system to obtain its subnet mask at bootstrap time. The requesting system broadcasts its ICMP request. (This is similar to a diskless system using RARP to obtain its IP address at bootstrap time.)

The identifier and sequence number fields in the ICMP message can be set to anything the sender chooses, and these values are returned in the reply. This allows the sender to match replies with requests.

Note that the sending host receives an ICMP reply even though nothing is seen on the wire. This is a general characteristic of broadcasting: the sending host receives a copy of the broadcast packet through some internal loopback mechanism. Since by definition the term "broadcast" means all the hosts on the local network, it should include the sending host.

Normally the reply should be unicast unless the source IP address of the request is 0.0.0.0.

ICMP Timestamp Request and Reply

The ICMP timestamp request allows a system to query another for the current time.

The requestor fills in the originate timestamp and sends the request. The replying system fills in the receive timestamp when it receives the request, and the transmit time-stamp when it sends the reply. In actuality, however, most implementations set the latter two fields to the same value. (The reason for providing the three fields is to let the sender compute the time for the request to be sent, and separately compute the time for the reply to be sent.)

ICMP Port Unreachable Error

ICMP Host and Network Unreachable Errors

The ICMP "host unreachable" error message is sent by a router when it receives an IP datagram that it cannot deliver or forward.

ICMP Unreachable Error (Fragmentation Required)

Another variation of the ICMP unreachable error occurs when a router receives a datagram that requires fragmentation, but the don't fragment (DF) flag is turned on in the IP header.

0001020304050607080910111213141516171819202122232425262728293031Type = 3CodeHeader checksumunusedNext-hop MTUIP header and first 8 bytes of original datagram's dataICMP Redirect Errors

The ICMP redirect error is sent by a router to the sender of an IP datagram when the datagram should have been sent to a different router.

  1. We assume that the host sends an IP datagram to Rl. This routing decision is often made because Rl is the default router for the host.
  2. Rl receives the datagram and performs a lookup in its routing table and determines that R2 is the correct next-hop router to send the datagram to. When it sends the datagram to R2, Rl detects that it is sending it out the same interface on which the datagram arrived (the LAN to which the host and the two routers are attached). This is the clue to a router that a redirect can be sent to the original sender.
  3. Rl sends an ICMP redirect to the host, telling it to send future datagrams to that destination to R2, instead of Rl.

A common use for redirects is to let a host with minimal routing knowledge build up a better routing table over time.The host can start with only a default route and anytime this default turns out to be wrong, it'll be informed by that default router with a redirect, allowing the host to update its routing table accordingly.

There are four different redirect messages, with different code values

CodeDescription0Redirect for Network1Redirect for Host2Redirect for Type of Service and Network3Redirect for Type of Service and Host
There are three IP addresses that the receiver of an ICMP redirect must look at:

  1. the IP address that caused the redirect (which is in the IP header returned as the data portion of the ICMP redirect)
  2. the IP address of the router that sent the redirect (which is the source IP address of the IP datagram containing the redirect), and
  3. the IP address of the router that should be used (which is in bytes 4-7 of the ICMP message).

There are numerous rules about ICMP redirects. First, redirects are generated only by routers, not by hosts. Also, redirects are intended to be used by hosts, not routers.

when acting as a router, performs the following checks, all of which must be true before an ICMP redirect is generated.

  1. The outgoing interface must equal the incoming interface.
  2. The route being used for the outgoing datagram must not have been created or modified by an ICMP redirect, and must not be the router's default route.
  3. The datagram must not be source routed.
  4. The kernel must be configured to send redirects.

host that receives an ICMP redirect performs some checks before modifying its routing table. These are to prevent a misbehaving router or host, or a malicious user, from incorrectly modifying a system's routing table.

  1. The new router must be on a directly connected network.
  2. The redirect must be from the current router for that destination.
  3. The redirect cannot tell the host to use itself as the router.
  4. The route that's being modified must be an indirect route.

Our final point about redirects is that routers should send only host redirects (codes 1 or 3 ) and not network redirects. Subnetting makes it hard to specify exactly when a network redirect can be sent instead of a host redirect. Some hosts treat a received network redirect as a host redirect, in case a router sends the wrong type.

ICMP Router Discovery Messages

One way to initialize a routing table is with static routes specified in configuration files. This is often used to set a default entry. A newer way is to use the ICMP router advertisement and solicitation messages.
The general concept is that after bootstrapping, a host broadcasts or multicasts a router solicitation message. One or more routers respond with a router advertisement message. Additionally, the routers periodically broadcast or multicast their router advertisements, allowing any hosts that are listening to update their routing table accordingly.


ICMP router solicitation message0001020304050607080910111213141516171819202122232425262728293031Type = 10Code = 0Header checksumUnused( set as 0 )

ICMP router advertisement message0001020304050607080910111213141516171819202122232425262728293031Type = 9CodeHeader checksumnumber of addressesaddress entry size = 2life timeroute address[ 1 ]preference level[ 1 ]route address[ 2 ]preference level[ 2 ]...Multiple addresses can be advertised by a router in a single message. Number of addresses is the number. Address entry size is the number of 32-bit words for each router address, and is always 2. Lifetime is the number of seconds that the advertised addresses can be considered valid.

One or more pairs of an IP address and a preference then follow. The IP address must be one of the sending router's IP addresses. The preference level is a signed 32-bit integer indicating the preference of this address as a default router address, relative to other router addresses on the same subnet. Larger values imply more preferable addresses. The preference level 0x80000000 means the corresponding address, although advertised, is not to be used by the receiver as a default router address. The default value of the preference is normally 0.

Router Operation

When a router starts up it transmits periodic advertisements on all interfaces capable of broadcasting or multicasting. These advertisements are not exactly periodic, but are randomized, to reduce the probability of synchronization with other routers on the same subnet. The normal time interval between advertisements is between 450 and 600 seconds. The default lifetime for a given advertisement is 30 minutes.

Another use of the lifetime field occurs when an interface on a router is disabled. In that case the router can transmit a final advertisement on the interface with the lifetime set to 0.

In addition to the periodic, unsolicited advertisements, a router also listens for solicitations from hosts. It responds to these solicitations with a router advertisement.

If there are multiple routers on a given subnet, it is up to the system administrator to configure the preference level for each router as appropriate. For example, the primary default router would have a higher preference than a backup.

Host Operation

Upon bootstrap a host normally transmits three router solicitations, 3 seconds apart. As soon as a valid advertisement is received, the solicitations stop.
A host also listens for advertisements from adjacent routers. These advertisements can cause the host's default router to change. Also, if an advertisement is not received for the current default, that default can time out.
As long as the normal default router stays up, that router will send advertisements every 10 minutes, with a lifetime of 30 minutes. This means the host's default entry won't time out, even if one or two advertisements are lost.

ICMP Source Quench Error

Using UDP we are also able to generate the ICMP "source quench" error. This is an error that may be generated by a system (router or host) when it receives datagrams at a rate that is too fast to be processed. Note the qualifier "may." A system is not required to send a source quench, even if it runs out of buffers and throws datagrams away.

0001020304050607080910111213141516171819202122232425262728293031Type = 4Code = 0Header checksumUnused( set as 0 )IP header (including options) + first 8 bytes of original IP datagram data


0 0
原创粉丝点击