分类: 系统运维
2012-05-14 11:39:46
We
call the ping program that sends the echo requests the client, and the
host being pinged the server. Most TCP/IP implementations support the
Ping server directly in the kernel-the server is not a user process.
(The two ICMP query services that we described in Chapter 6, the address
mask and timestamp requests, are also handled directly by the kernel.)
As
with other ICMP query messages, the server must echo the identifier and
sequence number fields. Also, any optional data sent by the client must
be echoed. These are presumably of interest to the client.
Unix
implementations of ping set the identifier field in the ICMP message to
the process ID of the sending process. This allows ping to identify the
returned responses if there are multiple instances of ping running at
the same time on the same host.
The sequence number starts at 0
and is incremented every time a new echo request is sent. ping prints
the sequence number of each returned packet, allowing us to see if
packets are missing, reordered, or duplicated. IP is a best effort
datagram delivery service, so any of these three conditions can occur.
Historically
the ping program has operated in a mode where it sends an echo request
once a second, printing each echo reply that is returned. Newer
implementations, however, require the -s option to operate this way. By
default, these newer implementations send only a single echo request
and output "host is alive" if an echo reply is received, or "no answer"
if no reply is received within 20 seconds.
IP Record Route Option
The ping program gives us an opportunity to look at the IP record route (RR) option. Most versions of ping provide the -R option that enables the record route feature. It causes ping to set the IP RR option in the outgoing IP datagram (which contains the ICMP echo request message). This causes every router that handles the datagram to add its IP address to a list in the options field. When the datagram reaches the final destination, the list of IP addresses should be copied into the outgoing ICMP echo reply, and all the routers on the return path also add their IP addresses to the list. When ping receives the echo reply it prints the list of IP addresses.
As simple as this sounds, there are
pitfalls. Generation of the RR option by the source host, processing of
the RR option by the intermediate routers, and reflection of the
incoming RR list in an ICMP echo request into the outgoing ICMP echo
reply are all optional features. Fortunately, most systems today do
support these optional features, but some systems don't reflect the IP
list.
The biggest problem, however, is the limited room in the IP
header for the list of IP addresses. The header length in the IP header
is a 4-bit field, limiting the entire IP header to 15 32-bit words (60
bytes). Since the fixed size of the IP header is 20 bytes, and the RR
option uses 3 bytes for overhead (which we describe below), this leaves
37 bytes (60-20-3) for the list, allowing up to nine IP addresses. In
the early days of the ARPANET, nine IP addresses seemed like a lot, but
since this is a round-trip list (in the case of the -R option for
ping), it's of limited use today. Despite these shortcomings, the
record route option works and provides an opportunity to look in detail
at the handling of IP options.
General format of the RR option in the IP datagram:
39bytes | |||||||
code | len | ptr | IP address #1 | IP address #2 | IP address #3 | ... | IP address #9 |
1 | 1 | 1 | 4 | 4 | 4 | 4 |
Code is a I-byte field specifying the type of IP option. For the RR option its value is 7.
Len is the total number of bytes of the RR option, which in this case is 39. (Although it's possible to specify an RR option with less than the maximum size, ping always provides a 39-byte option field, to record up to nine IP addresses. Given the limited room in the IP header for options, it doesn't make sense to specify a size less than the maximum.)
Ptr is called the pointer field. It is a 1-based index into the
39-byte option of where to store the next IP address. Its minimum value
is 4, which is the pointer to the first IP address. As each IP address
is recorded into the list, the value of ptr becomes 8, 12, 16, up to 36.
After the ninth address is recorded ptr becomes 40, indicating the list
is full.
When a router (which by definition is multihomed) records its IP address in the list, which IP address is recorded? It could be the address of the incoming interface or the outgoing interface. RFC 791 [Postel 1981a] specifies that the router records the outgoing IP address. We'll see that when the originating host (the host running ping) receives the ICMP echo reply with the RR option enabled, it also records its incoming IP address in the list.
IP Timestamp Option
The IP timestamp option is similar to the record route option. The format of the IP timestamp option:
40bytes | |||||||||
code | len | ptr | OF |
FL |
timestamp #1 | timestamp #2 | timestamp #3 | ... | timestamp #9 |
1 | 1 | 1 | 4 | 4 | 4 | 4 |
The code field is 0x44 for the timestamp option. The two fields len and ptr are the same as for the record route option: the total length of the option (normally 36 or 40) and a pointer to the next available entry (5,9, 13, etc.).
The next two fields are 4-bit values: OF is the overflow field and FL
is a flags field. The operation of the timestamp option is driven by
the flags field:
flags | Description |
0 | Record only timestamps. |
1 | Each router records its IP address and its timestamp. There is room for only four of these pairs in the options list. |
3 |
The sender initializes the options list with up to four pairs of IP addresses and a 0 timestamp. A router records its timestamp only if the next IP address in the list matches the router's. |
Given the limitations that we encountered with the record route
option, things get worse with the timestamp option. If we record both IP
addresses and timestamps (a flags of 1), we can store only four of
these pairs. Recording only timestamps is next to useless because we
have no indication regarding which timestamp corresponds to which router
(unless we have a fixed topology that never changes). A flags of 3 is
better, as we can then select which routers insert their timestamp.
A more fundamental problem is that you probably have no control over
how accurate the timestamp is at any given router. This makes it
fruitless to try to measure hop times between routers using this IP
option.