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 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636
|
/* $Id: send_packets.c 2008 2008-05-01 18:39:05Z aturner $ */
/*
* Copyright (c) 2001-2008 Aaron Turner.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the names of the copyright owners nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
* IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include "defines.h"
#include "common.h"
#include <sys/time.h>
#include <sys/types.h>
#include <signal.h>
#include <string.h>
#include <netinet/in.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include "tcpreplay.h"
#ifdef TCPREPLAY
#include "tcpreplay_opts.h"
#ifdef TCPREPLAY_EDIT
#include "tcpedit/tcpedit.h"
extern tcpedit_t *tcpedit;
#endif
#endif /* TCPREPLAY */
#include "send_packets.h"
#include "sleep.h"
extern tcpreplay_opt_t options;
extern struct timeval begin, end;
extern COUNTER bytes_sent, failed, pkts_sent;
extern volatile int didsig;
#ifdef DEBUG
extern int debug;
#endif
static void do_sleep(struct timeval *time, struct timeval *last, int len,
int accurate, sendpacket_t *sp, COUNTER counter, delta_t *ctx);
static const u_char *get_next_packet(pcap_t *pcap, struct pcap_pkthdr *pkthdr,
int file_idx, packet_cache_t **prev_packet);
static u_int32_t get_user_count(sendpacket_t *sp, COUNTER counter);
/**
* the main loop function for tcpreplay. This is where we figure out
* what to do with each packet
*/
void
send_packets(pcap_t *pcap, int cache_file_idx)
{
struct timeval last = { 0, 0 };
COUNTER packetnum = 0;
struct pcap_pkthdr pkthdr;
const u_char *pktdata = NULL;
sendpacket_t *sp = options.intf1;
u_int32_t pktlen;
packet_cache_t *cached_packet = NULL;
packet_cache_t **prev_packet = NULL;
#if defined TCPREPLAY && defined TCPREPLAY_EDIT
struct pcap_pkthdr *pkthdr_ptr;
#endif
delta_t delta_ctx;
/* register signals */
didsig = 0;
if (options.speed.mode != SPEED_ONEATATIME) {
(void)signal(SIGINT, catcher);
} else {
(void)signal(SIGINT, break_now);
}
if (options.enable_file_cache) {
prev_packet = &cached_packet;
} else {
prev_packet = NULL;
}
/* MAIN LOOP
* Keep sending while we have packets or until
* we've sent enough packets
*/
while ((pktdata = get_next_packet(pcap, &pkthdr, cache_file_idx, prev_packet)) != NULL) {
/* die? */
if (didsig)
break_now(0);
/* stop sending based on the limit -L? */
if (options.limit_send > 0 && pkts_sent >= options.limit_send)
return;
packetnum++;
#ifdef TCPREPLAY
/* do we use the snaplen (caplen) or the "actual" packet len? */
pktlen = HAVE_OPT(PKTLEN) ? pkthdr.len : pkthdr.caplen;
#elif TCPBRIDGE
pktlen = pkthdr.caplen;
#else
#error WTF??? We should not be here!
#endif
dbgx(2, "packet " COUNTER_SPEC " caplen %d", packetnum, pktlen);
/* Dual nic processing */
if (options.intf2 != NULL) {
sp = (sendpacket_t *) cache_mode(options.cachedata, packetnum);
/* sometimes we should not send the packet */
if (sp == TCPR_DIR_NOSEND)
continue;
}
/* do we need to print the packet via tcpdump? */
#ifdef ENABLE_VERBOSE
if (options.verbose)
tcpdump_print(options.tcpdump, &pkthdr, pktdata);
#endif
#if defined TCPREPLAY && defined TCPREPLAY_EDIT
pkthdr_ptr = &pkthdr;
if (tcpedit_packet(tcpedit, &pkthdr_ptr, &pktdata, sp->cache_dir) == -1) {
errx(1, "Error editing packet #" COUNTER_SPEC ": %s", packetnum, tcpedit_geterr(tcpedit));
}
pktlen = HAVE_OPT(PKTLEN) ? pkthdr_ptr->len : pkthdr_ptr->caplen;
#endif
/*
* we have to cast the ts, since OpenBSD sucks
* had to be special and use bpf_timeval.
* Only sleep if we're not in top speed mode (-t)
*/
if (options.speed.mode != SPEED_TOPSPEED)
do_sleep((struct timeval *)&pkthdr.ts, &last, pktlen, options.accurate, sp, packetnum, &delta_ctx);
/* mark the time when we send the packet */
start_delta_time(&delta_ctx);
/* write packet out on network */
if (sendpacket(sp, pktdata, pktlen) < (int)pktlen)
warnx("Unable to send packet: %s", sendpacket_geterr(sp));
/*
* track the time of the "last packet sent". Again, because of OpenBSD
* we have to do a mempcy rather then assignment.
*
* A number of 3rd party tools generate bad timestamps which go backwards
* in time. Hence, don't update the "last" unless pkthdr.ts > last
*/
if (timercmp(&last, &pkthdr.ts, <))
memcpy(&last, &pkthdr.ts, sizeof(struct timeval));
pkts_sent ++;
bytes_sent += pktlen;
} /* while */
if (options.enable_file_cache) {
options.file_cache[cache_file_idx].cached = TRUE;
}
}
/**
* Gets the next packet to be sent out. This will either read from the pcap file
* or will retrieve the packet from the internal cache.
*
* The parameter prev_packet is used as the parent of the new entry in the cache list.
* This should be NULL on the first call to this function for each file and
* will be updated as new entries are added (or retrieved) from the cache list.
*/
static const u_char *
get_next_packet(pcap_t *pcap, struct pcap_pkthdr *pkthdr, int file_idx,
packet_cache_t **prev_packet)
{
u_char *pktdata = NULL;
u_int32_t pktlen;
/* pcap may be null in cache mode! */
/* packet_cache_t may be null in file read mode! */
assert(pkthdr);
/*
* Check if we're caching files
*/
if (options.enable_file_cache && (prev_packet != NULL)) {
/*
* Yes we are caching files - has this one been cached?
*/
if (options.file_cache[file_idx].cached) {
if (*prev_packet == NULL) {
/*
* Get the first packet in the cache list directly from the file
*/
*prev_packet = options.file_cache[file_idx].packet_cache;
} else {
/*
* Get the next packet in the cache list
*/
*prev_packet = (*prev_packet)->next;
}
if (*prev_packet != NULL) {
pktdata = (*prev_packet)->pktdata;
memcpy(pkthdr, &((*prev_packet)->pkthdr), sizeof(struct pcap_pkthdr));
}
} else {
/*
* We should read the pcap file, and cache the results
*/
pktdata = (u_char *)pcap_next(pcap, pkthdr);
if (pktdata != NULL) {
if (*prev_packet == NULL) {
/*
* Create the first packet in the list
*/
*prev_packet = safe_malloc(sizeof(packet_cache_t));
options.file_cache[file_idx].packet_cache = *prev_packet;
} else {
/*
* Add a packet to the end of the list
*/
(*prev_packet)->next = safe_malloc(sizeof(packet_cache_t));
*prev_packet = (*prev_packet)->next;
}
if (*prev_packet != NULL) {
(*prev_packet)->next = NULL;
pktlen = pkthdr->len;
(*prev_packet)->pktdata = safe_malloc(pktlen);
memcpy((*prev_packet)->pktdata, pktdata, pktlen);
memcpy(&((*prev_packet)->pkthdr), pkthdr, sizeof(struct pcap_pkthdr));
}
}
}
} else {
/*
* Read pcap file as normal
*/
pktdata = (u_char *)pcap_next(pcap, pkthdr);
}
/* this get's casted to a const on the way out */
return pktdata;
}
/**
* determines based upon the cachedata which interface the given packet
* should go out. Also rewrites any layer 2 data we might need to adjust.
* Returns a void cased pointer to the options.intfX of the corresponding
* interface.
*/
void *
cache_mode(char *cachedata, COUNTER packet_num)
{
void *sp = NULL;
int result;
if (packet_num > options.cache_packets)
err(1, "Exceeded number of packets in cache file.");
result = check_cache(cachedata, packet_num);
if (result == TCPR_DIR_NOSEND) {
dbgx(2, "Cache: Not sending packet " COUNTER_SPEC ".", packet_num);
return TCPR_DIR_NOSEND;
}
else if (result == TCPR_DIR_C2S) {
dbgx(2, "Cache: Sending packet " COUNTER_SPEC " out primary interface.", packet_num);
sp = options.intf1;
}
else if (result == TCPR_DIR_S2C) {
dbgx(2, "Cache: Sending packet " COUNTER_SPEC " out secondary interface.", packet_num);
sp = options.intf2;
}
else {
err(1, "check_cache() returned an error. Aborting...");
}
return sp;
}
/**
* Given the timestamp on the current packet and the last packet sent,
* calculate the appropriate amount of time to sleep and do so.
*/
static void
do_sleep(struct timeval *time, struct timeval *last, int len, int accurate,
sendpacket_t *sp, COUNTER counter, delta_t *delta_ctx)
{
static struct timeval didsleep = { 0, 0 };
static struct timeval start = { 0, 0 };
#ifdef DEBUG
static struct timeval totalsleep = { 0, 0 };
#endif
struct timespec adjuster = { 0, 0 };
static struct timespec nap = { 0, 0 }, delta_time = {0, 0};
struct timeval nap_for, now, sleep_until;
struct timespec nap_this_time;
static int32_t nsec_adjuster = -1, nsec_times = -1;
float n;
static u_int32_t send = 0; /* accellerator. # of packets to send w/o sleeping */
u_int64_t ppnsec; /* packets per usec */
#ifdef TCPREPLAY
adjuster.tv_nsec = OPT_VALUE_SLEEP_ACCEL * 1000;
dbgx(2, "Adjuster: " TIMEVAL_FORMAT, adjuster.tv_sec, adjuster.tv_nsec);
#else
adjuster.tv_nsec = 0;
#endif
/* acclerator time? */
if (send > 0) {
send --;
return;
}
dbgx(4, "Last time: " TIMEVAL_FORMAT, last->tv_sec, last->tv_usec);
if (gettimeofday(&now, NULL) < 0)
errx(1, "Error gettimeofday: %s", strerror(errno));
dbgx(4, "Now time: " TIMEVAL_FORMAT, now.tv_sec, now.tv_usec);
/* First time through for this file */
if (pkts_sent == 0 || ((options.speed.mode != SPEED_MBPSRATE) && (counter == 0))) {
start = now;
timerclear(&sleep_until);
timerclear(&didsleep);
}
else {
timersub(&now, &start, &sleep_until);
}
switch(options.speed.mode) {
/*
* If top speed, you shouldn't even be here, but handle it anyways
*/
case SPEED_TOPSPEED:
notice("you shouldn't call do_sleep() in top speed mode.");
return;
break;
case SPEED_MULTIPLIER:
/*
* Replay packets a factor of the time they were originally sent.
*/
if (timerisset(last) && timercmp(time, last, >)) {
timersub(time, last, &nap_for);
TIMEVAL_TO_TIMESPEC(&nap_for, &nap);
timesdiv(&nap, options.speed.speed);
}
else {
/*
* Don't sleep if this is our first packet, or if the
* this packet appears to have been sent before the
* last packet.
*/
timesclear(&nap);
}
break;
case SPEED_MBPSRATE:
/*
* Ignore the time supplied by the capture file and send data at
* a constant 'rate' (bytes per second).
*/
if (pkts_sent != 0) {
n = (float)len / (options.speed.speed * 1024 * 1024 / 8); /* convert Mbps to bps */
nap.tv_sec = n;
nap.tv_nsec = (n - nap.tv_sec) * 1000000000;
dbgx(3, "packet size %d\t\tequals %f bps\t\tnap " TIMEVAL_FORMAT, len, n,
nap.tv_sec, nap.tv_nsec);
}
else {
/* don't sleep at all for the first packet */
timesclear(&nap);
}
break;
case SPEED_PACKETRATE:
/* only need to calculate this the first time */
if (! timesisset(&nap)) {
/* run in packets/sec */
ppnsec = 1000000000 / options.speed.speed;
NANOSEC_TO_TIMESPEC(ppnsec, &nap);
dbgx(1, "sending 1 packet per %lu nsec", nap.tv_nsec);
}
break;
case SPEED_ONEATATIME:
/* do we skip prompting for a key press? */
if (send == 0) {
send = get_user_count(sp, counter);
}
/* decrement our send counter */
printf("Sending packet " COUNTER_SPEC " out: %s\n", counter,
sp == options.intf1 ? options.intf1_name : options.intf2_name);
send --;
/* leave do_sleep() */
return;
break;
default:
errx(1, "Unknown/supported speed mode: %d", options.speed.mode);
break;
}
/*
* since we apply the adjuster to the sleep time, we can't modify nap
*/
nap_this_time.tv_sec = nap.tv_sec;
nap_this_time.tv_nsec = nap.tv_nsec;
if (accurate != ACCURATE_ABS_TIME) {
switch (options.speed.mode) {
/* Mbps & Multipler are dynamic timings, so we round to the nearest usec */
case SPEED_MBPSRATE:
case SPEED_MULTIPLIER:
ROUND_TIMESPEC_TO_MICROSEC(&nap_this_time);
break;
/* Packets/sec is static, so we weight packets for .1usec accuracy */
case SPEED_PACKETRATE:
if (nsec_adjuster < 0)
nsec_adjuster = (nap_this_time.tv_nsec % 10000) / 1000;
/* update in the range of 0-9 */
nsec_times = (nsec_times + 1) % 10;
if (nsec_times < nsec_adjuster) {
/* sorta looks like a no-op, but gives us a nice round usec number */
nap_this_time.tv_nsec = (nap_this_time.tv_nsec / 1000 * 1000) + 1000;
} else {
nap_this_time.tv_nsec -= (nap_this_time.tv_nsec % 1000);
}
dbgx(3, "(%ld)\tnsec_times = %ld\tnap adjust: %lu -> %lu", nsec_adjuster, nsec_times, nap.tv_nsec, nap_this_time.tv_nsec);
break;
default:
errx(1, "Unknown/supported speed mode: %d", options.speed.mode);
}
}
get_delta_time(delta_ctx, &delta_time);
dbgx(2, "delta: " TIMESPEC_FORMAT, delta_time.tv_sec, delta_time.tv_nsec);
if (timesisset(&delta_time)) {
if (timescmp(&nap_this_time, &delta_time, >)) {
timessub(&nap_this_time, &delta_time, &nap_this_time);
dbgx(3, "timesub: %lu %lu", delta_time.tv_sec, delta_time.tv_nsec);
} else {
timesclear(&nap_this_time);
dbgx(3, "timesclear: " TIMESPEC_FORMAT, delta_time.tv_sec, delta_time.tv_nsec);
}
}
/* apply the adjuster... */
if (timesisset(&adjuster)) {
if (timescmp(&nap_this_time, &adjuster, >)) {
timessub(&nap_this_time, &adjuster, &nap_this_time);
} else {
timesclear(&nap_this_time);
}
}
dbgx(2, "Sleeping: " TIMESPEC_FORMAT, nap_this_time.tv_sec, nap_this_time.tv_nsec);
/* don't sleep if nap = {0, 0} */
if (!timesisset(&nap_this_time))
return;
/*
* Depending on the accurate method & packet rate computation method
* We have multiple methods of sleeping, pick the right one...
*/
switch (accurate) {
#ifdef HAVE_SELECT
case ACCURATE_SELECT:
select_sleep(nap_this_time);
break;
#endif
#ifdef HAVE_IOPERM
case ACCURATE_IOPORT:
ioport_sleep(nap_this_time);
break;
#endif
#ifdef HAVE_RDTSC
case ACCURATE_RDTSC:
rdtsc_sleep(nap_this_time);
break;
#endif
#ifdef HAVE_ABSOLUTE_TIME
case ACCURATE_ABS_TIME:
absolute_time_sleep(nap_this_time);
break;
#endif
case ACCURATE_GTOD:
gettimeofday_sleep(nap_this_time);
break;
case ACCURATE_NANOSLEEP:
nanosleep_sleep(nap_this_time);
break;
/*
timeradd(&didsleep, &nap_this_time, &didsleep);
dbgx(4, "I will sleep " TIMEVAL_FORMAT, nap_this_time.tv_sec, nap_this_time.tv_usec);
if (timercmp(&didsleep, &sleep_until, >)) {
timersub(&didsleep, &sleep_until, &nap_this_time);
TIMEVAL_TO_TIMESPEC(&nap_this_time, &sleep);
dbgx(4, "Sleeping " TIMEVAL_FORMAT, nap_this_time.tv_sec, nap_this_time.tv_usec);
#ifdef DEBUG
timeradd(&totalsleep, &nap_this_time, &totalsleep);
#endif
if (nanosleep(&sleep, &ignore) == -1) {
warnx("nanosleep error: %s", strerror(errno));
}
}
break;
*/
default:
errx(1, "Unknown timer mode %d", accurate);
}
#ifdef DEBUG
dbgx(4, "Total sleep time: " TIMEVAL_FORMAT, totalsleep.tv_sec, totalsleep.tv_usec);
#endif
dbgx(2, "sleep delta: " TIMESPEC_FORMAT, delta_time.tv_sec, delta_time.tv_nsec);
}
/**
* this function will keep calling gettimeofday() until it returns
* >= time. This should be a lot more accurate then using nanosleep(),
* but at the cost of being more CPU intensive.
*/
static u_int32_t
get_user_count(sendpacket_t *sp, COUNTER counter)
{
struct pollfd poller[1]; /* use poll to read from the keyboard */
char input[EBUF_SIZE];
u_int32_t send = 0;
printf("**** Next packet #" COUNTER_SPEC " out %s. How many packets do you wish to send? ",
counter, (sp == options.intf1 ? options.intf1_name : options.intf2_name));
fflush(NULL);
poller[0].fd = STDIN_FILENO;
poller[0].events = POLLIN | POLLPRI | POLLNVAL;
poller[0].revents = 0;
if (fcntl(0, F_SETFL, fcntl(0, F_GETFL) & ~O_NONBLOCK))
errx(1, "Unable to clear non-blocking flag on stdin: %s", strerror(errno));
/* wait for the input */
if (poll(poller, 1, -1) < 0)
errx(1, "Error reading user input from stdin: %s", strerror(errno));
/*
* read to the end of the line or EBUF_SIZE,
* Note, if people are stupid, and type in more text then EBUF_SIZE
* then the next fgets() will pull in that data, which will have poor
* results. fuck them.
*/
if (fgets(input, sizeof(input), stdin) == NULL) {
errx(1, "Unable to process user input for fd %d: %s", fileno(stdin), strerror(errno));
} else if (strlen(input) > 1) {
send = strtoul(input, NULL, 0);
}
/* how many packets should we send? */
if (send == 0) {
dbg(1, "Input was less then 1 or non-numeric, assuming 1");
/* assume send only one packet */
send = 1;
}
return send;
}
/*
Local Variables:
mode:c
indent-tabs-mode:nil
c-basic-offset:4
End:
*/
|