1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508
|
static char rcsid[] = "$Id: PctestIpv4Icmp.cc 1082 2005-02-12 19:40:04Z bmah $";
//
// $Id: PctestIpv4Icmp.cc 1082 2005-02-12 19:40:04Z bmah $
//
// PctestIpv4Icmp.cc
// Bruce A. Mah <bmah@acm.org>
//
// This work was first produced by an employee of Sandia National
// Laboratories under a contract with the U.S. Department of Energy.
// Sandia National Laboratories dedicates whatever right, title or
// interest it may have in this software to the public. Although no
// license from Sandia is needed to copy and use this software,
// copying and using the software might infringe the rights of
// others. This software is provided as-is. SANDIA DISCLAIMS ANY
// WARRANTY OF ANY KIND, EXPRESS OR IMPLIED.
//
// Class of IPv4 tests using ICMP
//
#include <sys/types.h>
#include <netdb.h>
#include <netinet/in.h>
#include <netinet/in_systm.h>
#include <netinet/ip.h>
#include <netinet/ip_icmp.h>
#include <arpa/inet.h>
#ifdef HAVE_PCAP
#include <pcap.h>
#endif /* HAVE_PCAP */
#include "pc.h"
#include "PctestIpv4Icmp.h"
#include "TestRecord.h"
extern unsigned int Mtu;
//
// PctestIpv4Icmp::PctestIpv4Icmp
//
PctestIpv4Icmp::PctestIpv4Icmp()
{
extern bool PcapFlag;
#ifdef HAVE_PCAP
if (PcapFlag) {
// Initialize packet filter
//
// XXX Note that we will see both inbound and outbound packets
// with this filter rule. We *could* write up a new rule
// to add a src host predicate. XXX
if (pcap_compile(pc, &fp, "ip proto \\icmp", 1, maskp) < 0) {
fprintf(stderr, "pcap_compile failed\n");
exit(1);
}
if (pcap_setfilter(pc, &fp) < 0) {
fprintf(stderr, "pcap_setfilter failed\n");
exit(1);
}
}
#endif /* HAVE_PCAP */
}
//
// GetSocketOut
//
// Input: None
//
// Output: In return value, returns socket number.
//
// Get output socket of an appropriate type, store it in socketOut.
//
int PctestIpv4Icmp::GetSocketOut() {
int rc;
socketOut = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
if (socketOut < 0) {
perror("socket");
return socketOut;
}
int hdrinclOption;
hdrinclOption = 1;
rc = setsockopt(socketOut, IPPROTO_IP, IP_HDRINCL,
(const char *) &hdrinclOption,
sizeof(hdrinclOption));
if (rc < 0) {
perror("setsockopt(IP_HDRINCL)");
return rc;
}
// Bind remote side of socket
// (note that we need to have had PctestIpv4::SetTargetName()
// called first!
rc = connect(socketOut, (struct sockaddr *) &targetSocketAddress,
sizeof(struct sockaddr_in));
if (rc < 0) {
perror("connect");
return rc;
}
return socketOut;
}
//
// PctestIpv4Icmp::Test
//
// Input:
//
// Output:
//
// A negative icmpCode indicates an error.
//
int PctestIpv4Icmp::Test(TestRecord &tr)
{
struct timeval timeout;
int rc; // syscall return code
fd_set readFds; // reading file descriptors
int done = 0;
int i; // generic loop counter
// Parameters stored as globals
extern bool PcapFlag;
extern unsigned int Tos;
extern int Timeout;
// If the requested sending size is too small or too large,
// then return an error. The caller should have figured out the
// minimum sending size by calling Pctest::GetMinSize().
if ((tr.size < GetMinSize()) || (tr.size > IP_MAXPACKET)) {
return -1;
}
// Make up a ICMP packet to send out. Start with an IP header.
// See Section 25.3 of UNIX Network Programming, Second Edition,
// for why we need to twiddle the byte-orders of some fields but
// not others, depending on what OS we run.
struct ip ipHeader;
memset(&ipHeader, 0, sizeof(ipHeader));
#ifdef __osf__
// Tru64 <netinet/ip.h> does not declare ip_hl if __STDC__ == 1
ipHeader.ip_vhl = (sizeof(ip) >> 2) | (4 << 4);
#else
ipHeader.ip_hl = sizeof(ip) >> 2;
ipHeader.ip_v = 4;
#endif /* __osf__ */
ipHeader.ip_tos = Tos;
#ifdef linux
ipHeader.ip_len = htons(tr.size);
#else
ipHeader.ip_len = tr.size;
#endif /* linux */
ipHeader.ip_id = htons(0);
#ifdef linux
ipHeader.ip_off = htons(IP_DF);
#else
ipHeader.ip_off = IP_DF;
#endif /* linux */
ipHeader.ip_ttl = tr.hops;
ipHeader.ip_p = IPPROTO_ICMP;
ipHeader.ip_sum = 0;
memcpy(&(ipHeader.ip_src), &(originAddress), sizeof(struct in_addr));
memcpy(&(ipHeader.ip_dst), &(targetSocketAddress.sin_addr), sizeof(struct in_addr));
// Make up ICMP header.
int icmpPayloadSize = tr.size - sizeof(ip) - ICMP_MINLEN;
// need to hardcode size of headers for an ICMP
// echo request packet, because the associated
// structure is variable-sized.
struct icmp icmpHeader;
icmpHeader.icmp_type = ICMP_ECHO;
icmpHeader.icmp_code = 0;
icmpHeader.icmp_cksum = htons(0); // compute checksum
icmpHeader.icmp_id = htons(icmpId);
icmpHeader.icmp_seq = htons(icmpSequence++);
IF_DEBUG(2, fprintf(stdout, "test size %d, payload size %d\n", tr.size, icmpPayloadSize));
// ICMP payload
char *icmpPayload;
icmpPayload = GeneratePayload(icmpPayloadSize);
if (icmpPayload == NULL) {
fprintf(stderr, "Couldn't allocate space for payload\n");
return -1;
}
// Build the packet now.
char *ipPacket;
int ipPacketSize;
ipPacketSize = sizeof(ip) + ICMP_MINLEN + icmpPayloadSize;
ipPacket = new char[ipPacketSize];
memcpy(ipPacket, &ipHeader, sizeof(ipHeader));
memcpy(ipPacket + sizeof(ipHeader), &icmpHeader, ICMP_MINLEN);
memcpy(ipPacket + sizeof(ipHeader) + ICMP_MINLEN,
icmpPayload, icmpPayloadSize);
// Compute ICMP checksum. This is much simpler than the TCP or
// UDP checksums, because there is no pseudo-header.
u_int checksum;
checksum = (u_short) InCksum((u_short *) (ipPacket + sizeof(ipHeader)),
ICMP_MINLEN + icmpPayloadSize);
((icmp *)(ipPacket + sizeof(ipHeader)))->icmp_cksum = checksum;
// If we need to construct some advance packets, generate
// an image of said packet.
char *advancePacket;
if (tr.burst - 1 == 0) {
advancePacket = NULL;
}
else {
advancePacket = GenerateAdvancePacket(tr);
if (advancePacket == NULL) {
return -1;
}
}
// Use malloc(3) to allocate (memory-aligned) space for the inbound
// packet.
char *icmpPacket;
icmpPacket = (char *) malloc(IP_MAXPACKET);
if (icmpPacket == NULL) {
fprintf(stderr, "Couldn't allocate space for inbound packet\n");
return -1;
}
#ifdef HAVE_PCAP
if (!PcapFlag) {
#endif /* HAVE_PCAP */
// Set timeout value and socket select parameters
timeout.tv_sec = Timeout;
timeout.tv_usec = 0;
FD_ZERO(&readFds);
FD_SET(socketIn, &readFds);
#ifdef HAVE_PCAP
}
#endif /* HAVE_PCAP */
// Timestamp before
gettimeofday(&tvBefore, NULL);
// Send advance packets if necessary
for (i = 1; i < tr.burst; i++) {
rc = send(socketOut, advancePacket, Mtu, 0);
if (rc < 0) {
perror("send");
goto exittest;
}
}
tr.size += (tr.burst - 1) * Mtu;
// Send ICMP packet
rc = send(socketOut, ipPacket, ipPacketSize, 0);
if (rc < 0) {
perror("send");
goto exittest;
}
// We need to check the socket until we get a valid packet.
// So we might end up doing this select/read several times.
do {
#ifdef HAVE_PCAP
if (PcapFlag) {
// Wait for a packet. Only take in one packet at a time.
rc = pcap_dispatch(pc, 1, callback, (u_char *) this);
if (rc < 0) {
fprintf(stderr, "pcap_dispatch failed\n");
goto exittest;
}
// The callback will handle our after- timestamp if
// we get a packet, but if the read timeout fired first,
// we'll need to do a timestamp here to be sure that tvAfter
// has a valid value in it.
if (rc == 0) {
gettimeofday(&tvAfter, NULL);
}
}
else {
#endif /* HAVE_PCAP */
// Select and wait for an ICMP response or a timeout
rc = select(FD_SETSIZE, &readFds, NULL, NULL, &timeout);
if (rc < 0) {
perror("select");
goto exittest;
}
// Timestamp after and update test record timestamp fields
gettimeofday(&tvAfter, NULL);
#ifdef HAVE_PCAP
}
#endif /* HAVE_PCAP */
tr.tv.tv_sec = tvAfter.tv_sec - tvBefore.tv_sec - syscallTime.tv_sec;
tr.tv.tv_usec = tvAfter.tv_usec - tvBefore.tv_usec - syscallTime.tv_usec;
while (tr.tv.tv_usec < 0) {
tr.tv.tv_usec += 1000000;
tr.tv.tv_sec--;
}
// Read response from socket
if (rc == 1) {
IF_DEBUG(2, fprintf(stderr, "Response packet received\n"));
#ifdef HAVE_PCAP
if (PcapFlag) {
memcpy(icmpPacket, packet, packetLength);
tr.replsize = packetLength;
}
else {
#endif /* HAVE_PCAP */
rc = read(socketIn, icmpPacket, IP_MAXPACKET);
if (rc < 0) {
perror("read");
goto exittest;
}
tr.replsize = rc;
#ifdef HAVE_PCAP
}
#endif /* HAVE_PCAP */
// Now parse the packet, doing a little error checking along
// the way. By the end, we'll have ipHeader and icmpHeader
// pointing to valid structures within the packet, and
// ipHeader2 pointing to the IP header of the generating
// IP packet.
ip *ipHeaderIn, *ipHeaderIn2;
icmp *icmpHeaderIn, *icmpHeaderIn2;
unsigned int ipHeaderLength, ipHeaderLength2;
if (tr.replsize < sizeof(ip)) {
IF_DEBUG(3, fprintf(stderr, "Received incomplete IP packet of %d bytes\n", tr.replsize));
continue;
}
// Check protocol in IP header
ipHeaderIn = (ip *) icmpPacket;
if (ipHeaderIn->ip_p != proto) {
IF_DEBUG(0, fprintf(stderr, "Received unknown protocol %d in (supposedly) icmp packet\n", ipHeaderIn->ip_p));
rc = -1;
goto exittest;
}
#ifdef __osf__
// Tru64 <netinet/ip.h> doesn't declar ip_hl if __STDC__ == 1
ipHeaderLength = (ipHeaderIn->ip_vhl & 0x0f) << 2;
#else
ipHeaderLength = ipHeaderIn->ip_hl << 2;
#endif /* __osf__ */
if (tr.replsize - (0 + ipHeaderLength) < ICMP_MINLEN) {
IF_DEBUG(3, fprintf(stderr, "Received incomplete ICMP packet of %d bytes\n", tr.replsize));
continue;
}
icmpHeaderIn = (icmp *) (((char *) ipHeaderIn) + ipHeaderLength);
IF_DEBUG(3, fprintf(stderr, "icmp type = %d, code = %d\n",
icmpHeaderIn->icmp_type, icmpHeaderIn->icmp_code));
// Check ICMP type. Most types (such as echo request/reply,
// router adverts, etc.) we ignore.
if ((icmpHeaderIn->icmp_type != ICMP_TIMXCEED) &&
(icmpHeaderIn->icmp_type != ICMP_UNREACH) &&
(icmpHeaderIn->icmp_type != ICMP_ECHOREPLY)) {
IF_DEBUG(3, fprintf(stderr, "Ignoring ICMP packet\n"));
continue;
}
// Is it an echo reply? If so, see if it's a reply to
// something we might have sent.
if (icmpHeaderIn->icmp_type == ICMP_ECHOREPLY) {
if ((icmpHeaderIn->icmp_id != icmpHeader.icmp_id) ||
(icmpHeaderIn->icmp_seq != icmpHeader.icmp_seq)) {
IF_DEBUG(3, fprintf(stderr, "Ignoring ICMP packet with mismatched id/seq\n"));
continue;
}
tr.icmpSourceAddress = new char[sizeof(in_addr)];
memcpy(tr.icmpSourceAddress, &(ipHeaderIn->ip_src), sizeof(in_addr));
tr.icmpSourceAddressLength = sizeof(in_addr);
tr.result = GetAction(icmpHeaderIn->icmp_type,
icmpHeaderIn->icmp_code);
done = 1;
continue;
}
if (tr.replsize - (0 + ipHeaderLength + ICMP_MINLEN) <
sizeof(ip)) {
IF_DEBUG(3, fprintf(stderr, "Received incomplete inner IP packet, %d bytes total\n", tr.replsize));
continue;
}
// Check for a valid (to us) IP header within the packet.
// For "time exceeded" or "destination unreachable", this
// header will be right after the ICMP header.
ipHeaderIn2 = (ip *) ((char *) icmpHeaderIn + ICMP_MINLEN);
// Additional checking here...must be ICMP
if (ipHeaderIn2->ip_p != IPPROTO_ICMP) {
IF_DEBUG(3, fprintf(stderr, "Ignoring ICMP packet for non-ICMP\n"));
continue;
}
// Align ICMP header template.
#ifdef __osf__
// Tru64 <netinet/ip.h> doesn't declar ip_hl if __STDC__ == 1
ipHeaderLength2 = (ipHeaderIn2->ip_vhl & 0x0f) << 2;
#else
ipHeaderLength2 = ipHeaderIn2->ip_hl << 2;
#endif /* __osf__ */
if (tr.replsize - (0 + ipHeaderLength + ICMP_MINLEN + ipHeaderLength2) < ICMP_MINLEN) {
IF_DEBUG(3, fprintf(stderr, "Received incomplete inner ICMP packet, %d bytes total\n", tr.replsize));
continue;
}
icmpHeaderIn2 = (icmp *) (((char *) ipHeaderIn2) + ipHeaderLength2);
if ((icmpHeaderIn2->icmp_id != icmpHeader.icmp_id) ||
(icmpHeaderIn2->icmp_seq != icmpHeader.icmp_seq)) {
IF_DEBUG(3, fprintf(stderr, "Ignoring inner ICMP packet with mismatched id/seq\n"));
continue;
}
// Fill in return fields
tr.icmpSourceAddress = new char[sizeof(in_addr)];
memcpy(tr.icmpSourceAddress, &(ipHeaderIn->ip_src), sizeof(in_addr));
tr.icmpSourceAddressLength = sizeof(in_addr);
tr.result = GetAction(icmpHeaderIn->icmp_type,
icmpHeaderIn->icmp_code);
done = 1;
}
// If we didn't get a packet yet, see if we've waited too long
else if (tr.tv.tv_sec >= Timeout) {
IF_DEBUG(2, fprintf(stderr, "Timeout\n"));
tr.icmpSourceAddress = new char[sizeof(in_addr)];
memset(tr.icmpSourceAddress, 0, sizeof(in_addr));
tr.icmpSourceAddressLength = sizeof(in_addr);
tr.result = PctestActionTimeout;
done = 1;
}
} while (!done);
rc = 0;
exittest:
if (advancePacket) {
delete [] advancePacket;
}
delete [] icmpPayload;
delete [] ipPacket;
free(icmpPacket);
return rc;
}
//
// PctestIpv4Icmp::GetMinSize
//
// Input: None
//
// Output: Minimum packet size possible for this protocol (in return
// value).
//
unsigned int PctestIpv4Icmp::GetMinSize()
{
return (sizeof(ip) + ICMP_MINLEN + 4); // need to hardcode size of an ICMP
// echo request packet, because the associated
// structure is variable-sized.
}
//
// PctestIpv4Icmp::GetAction
//
// Input: a test record
//
// Output: action code
//
// Figure out the meaning of a particular combination of ICMPv4 type
// and code values.
//
PctestActionType PctestIpv4Icmp::GetAction(int icmpType, int icmpCode)
{
if (icmpType == ICMP_TIMXCEED) {
return PctestActionValid;
}
else if (icmpType == ICMP_ECHOREPLY) {
return PctestActionValidLasthop;
}
else if ((icmpType == ICMP_UNREACH) &&
(icmpCode == ICMP_UNREACH_FILTER_PROHIB)) {
return PctestActionFiltered;
}
else {
return PctestActionAbort;
}
}
|