iptests.c revision 172776
1/* $FreeBSD: head/contrib/ipfilter/ipsend/iptests.c 172776 2007-10-18 21:52:14Z darrenr $ */ 2 3/* 4 * Copyright (C) 1993-1998 by Darren Reed. 5 * 6 * See the IPFILTER.LICENCE file for details on licencing. 7 * 8 */ 9#if !defined(lint) 10static const char sccsid[] = "%W% %G% (C)1995 Darren Reed"; 11static const char rcsid[] = "@(#)$Id: iptests.c,v 2.8.2.9 2007/09/13 07:19:34 darrenr Exp $"; 12#endif 13#include <sys/param.h> 14#include <sys/types.h> 15#if defined(__NetBSD__) && defined(__vax__) 16/* 17 * XXX need to declare boolean_t for _KERNEL <sys/files.h> 18 * which ends up including <sys/device.h> for vax. See PR#32907 19 * for further details. 20 */ 21typedef int boolean_t; 22#endif 23#include <sys/time.h> 24#if !defined(__osf__) 25# ifdef __NetBSD__ 26# include <machine/lock.h> 27# endif 28# define _KERNEL 29# define KERNEL 30# if !defined(solaris) && !defined(linux) && !defined(__sgi) && !defined(hpux) 31# include <sys/file.h> 32# else 33# ifdef solaris 34# include <sys/dditypes.h> 35# endif 36# endif 37# undef _KERNEL 38# undef KERNEL 39#endif 40#if !defined(solaris) && !defined(linux) && !defined(__sgi) 41# include <nlist.h> 42# include <sys/user.h> 43# include <sys/proc.h> 44#endif 45#if !defined(ultrix) && !defined(hpux) && !defined(linux) && \ 46 !defined(__sgi) && !defined(__osf__) && !defined(_AIX51) 47# include <kvm.h> 48#endif 49#ifndef ultrix 50# include <sys/socket.h> 51#endif 52#if defined(solaris) 53# include <sys/stream.h> 54#endif 55#include <sys/socketvar.h> 56#ifdef sun 57#include <sys/systm.h> 58#include <sys/session.h> 59#endif 60#if BSD >= 199103 61# include <sys/sysctl.h> 62# include <sys/filedesc.h> 63# include <paths.h> 64#endif 65#include <netinet/in_systm.h> 66#include <sys/socket.h> 67#ifdef __hpux 68# define _NET_ROUTE_INCLUDED 69#endif 70#ifdef __osf__ 71# include "radix_ipf_local.h" 72#endif 73#include <net/if.h> 74#if defined(linux) && (LINUX >= 0200) 75# include <asm/atomic.h> 76#endif 77#if !defined(linux) 78# if defined(__FreeBSD__) 79# include "radix_ipf.h" 80# endif 81# include <net/route.h> 82#else 83# define __KERNEL__ /* because there's a macro not wrapped by this */ 84# include <net/route.h> /* in this file :-/ */ 85#endif 86#include <netinet/in.h> 87#include <arpa/inet.h> 88#include <netinet/ip.h> 89#if !defined(linux) 90# include <netinet/ip_var.h> 91# if !defined(__hpux) 92# include <netinet/in_pcb.h> 93# endif 94#endif 95#if defined(__SVR4) || defined(__svr4__) || defined(__sgi) 96# include <sys/sysmacros.h> 97#endif 98#include <stdio.h> 99#include <unistd.h> 100#include <stdlib.h> 101#include <string.h> 102#ifdef __hpux 103# undef _NET_ROUTE_INCLUDED 104#endif 105#include "ipsend.h" 106#if !defined(linux) && !defined(__hpux) 107# include <netinet/tcp_timer.h> 108# include <netinet/tcp_var.h> 109#endif 110#if defined(__NetBSD_Version__) && (__NetBSD_Version__ >= 106000000) 111# define USE_NANOSLEEP 112#endif 113 114 115#ifdef USE_NANOSLEEP 116# define PAUSE() ts.tv_sec = 0; ts.tv_nsec = 10000000; \ 117 (void) nanosleep(&ts, NULL) 118#else 119# define PAUSE() tv.tv_sec = 0; tv.tv_usec = 10000; \ 120 (void) select(0, NULL, NULL, NULL, &tv) 121#endif 122 123 124void ip_test1(dev, mtu, ip, gwip, ptest) 125char *dev; 126int mtu; 127ip_t *ip; 128struct in_addr gwip; 129int ptest; 130{ 131#ifdef USE_NANOSLEEP 132 struct timespec ts; 133#else 134 struct timeval tv; 135#endif 136 udphdr_t *u; 137 int nfd, i = 0, len, id = getpid(); 138 139 IP_HL_A(ip, sizeof(*ip) >> 2); 140 IP_V_A(ip, IPVERSION); 141 ip->ip_tos = 0; 142 ip->ip_off = 0; 143 ip->ip_ttl = 60; 144 ip->ip_p = IPPROTO_UDP; 145 ip->ip_sum = 0; 146 u = (udphdr_t *)(ip + 1); 147 u->uh_sport = htons(1); 148 u->uh_dport = htons(9); 149 u->uh_sum = 0; 150 u->uh_ulen = htons(sizeof(*u) + 4); 151 ip->ip_len = sizeof(*ip) + ntohs(u->uh_ulen); 152 len = ip->ip_len; 153 154 nfd = initdevice(dev, 1); 155 if (nfd == -1) 156 return; 157 158 if (!ptest || (ptest == 1)) { 159 /* 160 * Part1: hl < len 161 */ 162 ip->ip_id = 0; 163 printf("1.1. sending packets with ip_hl < ip_len\n"); 164 for (i = 0; i < ((sizeof(*ip) + ntohs(u->uh_ulen)) >> 2); i++) { 165 IP_HL_A(ip, i >> 2); 166 (void) send_ip(nfd, 1500, ip, gwip, 1); 167 printf("%d\r", i); 168 fflush(stdout); 169 PAUSE(); 170 } 171 putchar('\n'); 172 } 173 174 if (!ptest || (ptest == 2)) { 175 /* 176 * Part2: hl > len 177 */ 178 ip->ip_id = 0; 179 printf("1.2. sending packets with ip_hl > ip_len\n"); 180 for (; i < ((sizeof(*ip) * 2 + ntohs(u->uh_ulen)) >> 2); i++) { 181 IP_HL_A(ip, i >> 2); 182 (void) send_ip(nfd, 1500, ip, gwip, 1); 183 printf("%d\r", i); 184 fflush(stdout); 185 PAUSE(); 186 } 187 putchar('\n'); 188 } 189 190 if (!ptest || (ptest == 3)) { 191 /* 192 * Part3: v < 4 193 */ 194 ip->ip_id = 0; 195 printf("1.3. ip_v < 4\n"); 196 IP_HL_A(ip, sizeof(*ip) >> 2); 197 for (i = 0; i < 4; i++) { 198 IP_V_A(ip, i); 199 (void) send_ip(nfd, 1500, ip, gwip, 1); 200 printf("%d\r", i); 201 fflush(stdout); 202 PAUSE(); 203 } 204 putchar('\n'); 205 } 206 207 if (!ptest || (ptest == 4)) { 208 /* 209 * Part4: v > 4 210 */ 211 ip->ip_id = 0; 212 printf("1.4. ip_v > 4\n"); 213 for (i = 5; i < 16; i++) { 214 IP_V_A(ip, i); 215 (void) send_ip(nfd, 1500, ip, gwip, 1); 216 printf("%d\r", i); 217 fflush(stdout); 218 PAUSE(); 219 } 220 putchar('\n'); 221 } 222 223 if (!ptest || (ptest == 5)) { 224 /* 225 * Part5: len < packet 226 */ 227 ip->ip_id = 0; 228 IP_V_A(ip, IPVERSION); 229 i = ip->ip_len + 1; 230 printf("1.5.0 ip_len < packet size (size++, long packets)\n"); 231 for (; i < (ip->ip_len * 2); i++) { 232 ip->ip_id = htons(id++); 233 ip->ip_sum = 0; 234 ip->ip_sum = chksum((u_short *)ip, IP_HL(ip) << 2); 235 (void) send_ether(nfd, (char *)ip, i, gwip); 236 printf("%d\r", i); 237 fflush(stdout); 238 PAUSE(); 239 } 240 putchar('\n'); 241 printf("1.5.1 ip_len < packet size (ip_len-, short packets)\n"); 242 for (i = len; i > 0; i--) { 243 ip->ip_id = htons(id++); 244 ip->ip_len = i; 245 ip->ip_sum = 0; 246 ip->ip_sum = chksum((u_short *)ip, IP_HL(ip) << 2); 247 (void) send_ether(nfd, (char *)ip, len, gwip); 248 printf("%d\r", i); 249 fflush(stdout); 250 PAUSE(); 251 } 252 putchar('\n'); 253 } 254 255 if (!ptest || (ptest == 6)) { 256 /* 257 * Part6: len > packet 258 */ 259 ip->ip_id = 0; 260 printf("1.6.0 ip_len > packet size (increase ip_len)\n"); 261 for (i = len + 1; i < (len * 2); i++) { 262 ip->ip_id = htons(id++); 263 ip->ip_len = i; 264 ip->ip_sum = 0; 265 ip->ip_sum = chksum((u_short *)ip, IP_HL(ip) << 2); 266 (void) send_ether(nfd, (char *)ip, len, gwip); 267 printf("%d\r", i); 268 fflush(stdout); 269 PAUSE(); 270 } 271 putchar('\n'); 272 ip->ip_len = len; 273 printf("1.6.1 ip_len > packet size (size--, short packets)\n"); 274 for (i = len; i > 0; i--) { 275 ip->ip_id = htons(id++); 276 ip->ip_sum = 0; 277 ip->ip_sum = chksum((u_short *)ip, IP_HL(ip) << 2); 278 (void) send_ether(nfd, (char *)ip, i, gwip); 279 printf("%d\r", i); 280 fflush(stdout); 281 PAUSE(); 282 } 283 putchar('\n'); 284 } 285 286 if (!ptest || (ptest == 7)) { 287 /* 288 * Part7: 0 length fragment 289 */ 290 printf("1.7.0 Zero length fragments (ip_off = 0x2000)\n"); 291 ip->ip_id = 0; 292 ip->ip_len = sizeof(*ip); 293 ip->ip_off = htons(IP_MF); 294 (void) send_ip(nfd, mtu, ip, gwip, 1); 295 fflush(stdout); 296 PAUSE(); 297 298 printf("1.7.1 Zero length fragments (ip_off = 0x3000)\n"); 299 ip->ip_id = 0; 300 ip->ip_len = sizeof(*ip); 301 ip->ip_off = htons(IP_MF); 302 (void) send_ip(nfd, mtu, ip, gwip, 1); 303 fflush(stdout); 304 PAUSE(); 305 306 printf("1.7.2 Zero length fragments (ip_off = 0xa000)\n"); 307 ip->ip_id = 0; 308 ip->ip_len = sizeof(*ip); 309 ip->ip_off = htons(0xa000); 310 (void) send_ip(nfd, mtu, ip, gwip, 1); 311 fflush(stdout); 312 PAUSE(); 313 314 printf("1.7.3 Zero length fragments (ip_off = 0x0100)\n"); 315 ip->ip_id = 0; 316 ip->ip_len = sizeof(*ip); 317 ip->ip_off = htons(0x0100); 318 (void) send_ip(nfd, mtu, ip, gwip, 1); 319 fflush(stdout); 320 PAUSE(); 321 } 322 323 if (!ptest || (ptest == 8)) { 324 struct timeval tv; 325 326 gettimeofday(&tv, NULL); 327 srand(tv.tv_sec ^ getpid() ^ tv.tv_usec); 328 /* 329 * Part8.1: 63k packet + 1k fragment at offset 0x1ffe 330 * Mark it as being ICMP (so it doesn't get junked), but 331 * don't bother about the ICMP header, we're not worrying 332 * about that here. 333 */ 334 ip->ip_p = IPPROTO_ICMP; 335 ip->ip_off = htons(IP_MF); 336 u->uh_dport = htons(9); 337 ip->ip_id = htons(id++); 338 printf("1.8.1 63k packet + 1k fragment at offset 0x1ffe\n"); 339 ip->ip_len = 768 + 20 + 8; 340 (void) send_ip(nfd, mtu, ip, gwip, 1); 341 printf("%d\r", i); 342 343 ip->ip_len = MIN(768 + 20, mtu - 68); 344 i = 512; 345 for (; i < (63 * 1024 + 768); i += 768) { 346 ip->ip_off = htons(IP_MF | (i >> 3)); 347 (void) send_ip(nfd, mtu, ip, gwip, 1); 348 printf("%d\r", i); 349 fflush(stdout); 350 PAUSE(); 351 } 352 ip->ip_len = 896 + 20; 353 ip->ip_off = htons(i >> 3); 354 (void) send_ip(nfd, mtu, ip, gwip, 1); 355 printf("%d\r", i); 356 putchar('\n'); 357 fflush(stdout); 358 359 /* 360 * Part8.2: 63k packet + 1k fragment at offset 0x1ffe 361 * Mark it as being ICMP (so it doesn't get junked), but 362 * don't bother about the ICMP header, we're not worrying 363 * about that here. (Lossage here) 364 */ 365 ip->ip_p = IPPROTO_ICMP; 366 ip->ip_off = htons(IP_MF); 367 u->uh_dport = htons(9); 368 ip->ip_id = htons(id++); 369 printf("1.8.2 63k packet + 1k fragment at offset 0x1ffe\n"); 370 ip->ip_len = 768 + 20 + 8; 371 if ((rand() & 0x1f) != 0) { 372 (void) send_ip(nfd, mtu, ip, gwip, 1); 373 printf("%d\r", i); 374 } else 375 printf("skip 0\n"); 376 377 ip->ip_len = MIN(768 + 20, mtu - 68); 378 i = 512; 379 for (; i < (63 * 1024 + 768); i += 768) { 380 ip->ip_off = htons(IP_MF | (i >> 3)); 381 if ((rand() & 0x1f) != 0) { 382 (void) send_ip(nfd, mtu, ip, gwip, 1); 383 printf("%d\r", i); 384 } else 385 printf("skip %d\n", i); 386 fflush(stdout); 387 PAUSE(); 388 } 389 ip->ip_len = 896 + 20; 390 ip->ip_off = htons(i >> 3); 391 if ((rand() & 0x1f) != 0) { 392 (void) send_ip(nfd, mtu, ip, gwip, 1); 393 printf("%d\r", i); 394 } else 395 printf("skip\n"); 396 putchar('\n'); 397 fflush(stdout); 398 399 /* 400 * Part8.3: 33k packet - test for not dealing with -ve length 401 * Mark it as being ICMP (so it doesn't get junked), but 402 * don't bother about the ICMP header, we're not worrying 403 * about that here. 404 */ 405 ip->ip_p = IPPROTO_ICMP; 406 ip->ip_off = htons(IP_MF); 407 u->uh_dport = htons(9); 408 ip->ip_id = htons(id++); 409 printf("1.8.3 33k packet\n"); 410 ip->ip_len = 768 + 20 + 8; 411 (void) send_ip(nfd, mtu, ip, gwip, 1); 412 printf("%d\r", i); 413 414 ip->ip_len = MIN(768 + 20, mtu - 68); 415 i = 512; 416 for (; i < (32 * 1024 + 768); i += 768) { 417 ip->ip_off = htons(IP_MF | (i >> 3)); 418 (void) send_ip(nfd, mtu, ip, gwip, 1); 419 printf("%d\r", i); 420 fflush(stdout); 421 PAUSE(); 422 } 423 ip->ip_len = 896 + 20; 424 ip->ip_off = htons(i >> 3); 425 (void) send_ip(nfd, mtu, ip, gwip, 1); 426 printf("%d\r", i); 427 putchar('\n'); 428 fflush(stdout); 429 } 430 431 ip->ip_len = len; 432 ip->ip_off = 0; 433 if (!ptest || (ptest == 9)) { 434 /* 435 * Part9: off & 0x8000 == 0x8000 436 */ 437 ip->ip_id = 0; 438 ip->ip_off = htons(0x8000); 439 printf("1.9. ip_off & 0x8000 == 0x8000\n"); 440 (void) send_ip(nfd, mtu, ip, gwip, 1); 441 fflush(stdout); 442 PAUSE(); 443 } 444 445 ip->ip_off = 0; 446 447 if (!ptest || (ptest == 10)) { 448 /* 449 * Part10: ttl = 255 450 */ 451 ip->ip_id = 0; 452 ip->ip_ttl = 255; 453 printf("1.10.0 ip_ttl = 255\n"); 454 (void) send_ip(nfd, mtu, ip, gwip, 1); 455 fflush(stdout); 456 PAUSE(); 457 458 ip->ip_ttl = 128; 459 printf("1.10.1 ip_ttl = 128\n"); 460 (void) send_ip(nfd, mtu, ip, gwip, 1); 461 fflush(stdout); 462 PAUSE(); 463 464 ip->ip_ttl = 0; 465 printf("1.10.2 ip_ttl = 0\n"); 466 (void) send_ip(nfd, mtu, ip, gwip, 1); 467 fflush(stdout); 468 PAUSE(); 469 } 470 471 (void) close(nfd); 472} 473 474 475void ip_test2(dev, mtu, ip, gwip, ptest) 476char *dev; 477int mtu; 478ip_t *ip; 479struct in_addr gwip; 480int ptest; 481{ 482#ifdef USE_NANOSLEEP 483 struct timespec ts; 484#else 485 struct timeval tv; 486#endif 487 int nfd; 488 u_char *s; 489 490 491 nfd = initdevice(dev, 1); 492 if (nfd == -1) 493 return; 494 495 IP_HL_A(ip, 6); 496 ip->ip_len = IP_HL(ip) << 2; 497 s = (u_char *)(ip + 1); 498 s[IPOPT_OPTVAL] = IPOPT_NOP; 499 s++; 500 if (!ptest || (ptest == 1)) { 501 /* 502 * Test 1: option length > packet length, 503 * header length == packet length 504 */ 505 s[IPOPT_OPTVAL] = IPOPT_TS; 506 s[IPOPT_OLEN] = 4; 507 s[IPOPT_OFFSET] = IPOPT_MINOFF; 508 ip->ip_p = IPPROTO_IP; 509 printf("2.1 option length > packet length\n"); 510 (void) send_ip(nfd, mtu, ip, gwip, 1); 511 fflush(stdout); 512 PAUSE(); 513 } 514 515 IP_HL_A(ip, 7); 516 ip->ip_len = IP_HL(ip) << 2; 517 if (!ptest || (ptest == 1)) { 518 /* 519 * Test 2: options have length = 0 520 */ 521 printf("2.2.1 option length = 0, RR\n"); 522 s[IPOPT_OPTVAL] = IPOPT_RR; 523 s[IPOPT_OLEN] = 0; 524 (void) send_ip(nfd, mtu, ip, gwip, 1); 525 fflush(stdout); 526 PAUSE(); 527 528 printf("2.2.2 option length = 0, TS\n"); 529 s[IPOPT_OPTVAL] = IPOPT_TS; 530 s[IPOPT_OLEN] = 0; 531 (void) send_ip(nfd, mtu, ip, gwip, 1); 532 fflush(stdout); 533 PAUSE(); 534 535 printf("2.2.3 option length = 0, SECURITY\n"); 536 s[IPOPT_OPTVAL] = IPOPT_SECURITY; 537 s[IPOPT_OLEN] = 0; 538 (void) send_ip(nfd, mtu, ip, gwip, 1); 539 fflush(stdout); 540 PAUSE(); 541 542 printf("2.2.4 option length = 0, LSRR\n"); 543 s[IPOPT_OPTVAL] = IPOPT_LSRR; 544 s[IPOPT_OLEN] = 0; 545 (void) send_ip(nfd, mtu, ip, gwip, 1); 546 fflush(stdout); 547 PAUSE(); 548 549 printf("2.2.5 option length = 0, SATID\n"); 550 s[IPOPT_OPTVAL] = IPOPT_SATID; 551 s[IPOPT_OLEN] = 0; 552 (void) send_ip(nfd, mtu, ip, gwip, 1); 553 fflush(stdout); 554 PAUSE(); 555 556 printf("2.2.6 option length = 0, SSRR\n"); 557 s[IPOPT_OPTVAL] = IPOPT_SSRR; 558 s[IPOPT_OLEN] = 0; 559 (void) send_ip(nfd, mtu, ip, gwip, 1); 560 fflush(stdout); 561 PAUSE(); 562 } 563 564 (void) close(nfd); 565} 566 567 568/* 569 * test 3 (ICMP) 570 */ 571void ip_test3(dev, mtu, ip, gwip, ptest) 572char *dev; 573int mtu; 574ip_t *ip; 575struct in_addr gwip; 576int ptest; 577{ 578 static int ict1[10] = { 8, 9, 10, 13, 14, 15, 16, 17, 18, 0 }; 579 static int ict2[8] = { 3, 9, 10, 13, 14, 17, 18, 0 }; 580#ifdef USE_NANOSLEEP 581 struct timespec ts; 582#else 583 struct timeval tv; 584#endif 585 struct icmp *icp; 586 int nfd, i; 587 588 IP_HL_A(ip, sizeof(*ip) >> 2); 589 IP_V_A(ip, IPVERSION); 590 ip->ip_tos = 0; 591 ip->ip_off = 0; 592 ip->ip_ttl = 60; 593 ip->ip_p = IPPROTO_ICMP; 594 ip->ip_sum = 0; 595 ip->ip_len = sizeof(*ip) + sizeof(*icp); 596 icp = (struct icmp *)((char *)ip + (IP_HL(ip) << 2)); 597 598 nfd = initdevice(dev, 1); 599 if (nfd == -1) 600 return; 601 602 if (!ptest || (ptest == 1)) { 603 /* 604 * Type 0 - 31, 255, code = 0 605 */ 606 bzero((char *)icp, sizeof(*icp)); 607 for (i = 0; i < 32; i++) { 608 icp->icmp_type = i; 609 (void) send_icmp(nfd, mtu, ip, gwip); 610 PAUSE(); 611 printf("3.1.%d ICMP type %d code 0 (all 0's)\r", i, i); 612 } 613 icp->icmp_type = 255; 614 (void) send_icmp(nfd, mtu, ip, gwip); 615 PAUSE(); 616 printf("3.1.%d ICMP type %d code 0 (all 0's)\r", i, 255); 617 putchar('\n'); 618 } 619 620 if (!ptest || (ptest == 2)) { 621 /* 622 * Type 3, code = 0 - 31 623 */ 624 icp->icmp_type = 3; 625 for (i = 0; i < 32; i++) { 626 icp->icmp_code = i; 627 (void) send_icmp(nfd, mtu, ip, gwip); 628 PAUSE(); 629 printf("3.2.%d ICMP type 3 code %d (all 0's)\r", i, i); 630 } 631 } 632 633 if (!ptest || (ptest == 3)) { 634 /* 635 * Type 4, code = 0,127,128,255 636 */ 637 icp->icmp_type = 4; 638 icp->icmp_code = 0; 639 (void) send_icmp(nfd, mtu, ip, gwip); 640 PAUSE(); 641 printf("3.3.1 ICMP type 4 code 0 (all 0's)\r"); 642 icp->icmp_code = 127; 643 (void) send_icmp(nfd, mtu, ip, gwip); 644 PAUSE(); 645 printf("3.3.2 ICMP type 4 code 127 (all 0's)\r"); 646 icp->icmp_code = 128; 647 (void) send_icmp(nfd, mtu, ip, gwip); 648 PAUSE(); 649 printf("3.3.3 ICMP type 4 code 128 (all 0's)\r"); 650 icp->icmp_code = 255; 651 (void) send_icmp(nfd, mtu, ip, gwip); 652 PAUSE(); 653 printf("3.3.4 ICMP type 4 code 255 (all 0's)\r"); 654 } 655 656 if (!ptest || (ptest == 4)) { 657 /* 658 * Type 5, code = 0,127,128,255 659 */ 660 icp->icmp_type = 5; 661 icp->icmp_code = 0; 662 (void) send_icmp(nfd, mtu, ip, gwip); 663 PAUSE(); 664 printf("3.4.1 ICMP type 5 code 0 (all 0's)\r"); 665 icp->icmp_code = 127; 666 (void) send_icmp(nfd, mtu, ip, gwip); 667 PAUSE(); 668 printf("3.4.2 ICMP type 5 code 127 (all 0's)\r"); 669 icp->icmp_code = 128; 670 (void) send_icmp(nfd, mtu, ip, gwip); 671 PAUSE(); 672 printf("3.4.3 ICMP type 5 code 128 (all 0's)\r"); 673 icp->icmp_code = 255; 674 (void) send_icmp(nfd, mtu, ip, gwip); 675 PAUSE(); 676 printf("3.4.4 ICMP type 5 code 255 (all 0's)\r"); 677 } 678 679 if (!ptest || (ptest == 5)) { 680 /* 681 * Type 8-10;13-18, code - 0,127,128,255 682 */ 683 for (i = 0; ict1[i]; i++) { 684 icp->icmp_type = ict1[i]; 685 icp->icmp_code = 0; 686 (void) send_icmp(nfd, mtu, ip, gwip); 687 PAUSE(); 688 printf("3.5.%d ICMP type 5 code 0 (all 0's)\r", 689 i * 4); 690 icp->icmp_code = 127; 691 (void) send_icmp(nfd, mtu, ip, gwip); 692 PAUSE(); 693 printf("3.5.%d ICMP type 5 code 127 (all 0's)\r", 694 i * 4 + 1); 695 icp->icmp_code = 128; 696 (void) send_icmp(nfd, mtu, ip, gwip); 697 PAUSE(); 698 printf("3.5.%d ICMP type 5 code 128 (all 0's)\r", 699 i * 4 + 2); 700 icp->icmp_code = 255; 701 (void) send_icmp(nfd, mtu, ip, gwip); 702 PAUSE(); 703 printf("3.5.%d ICMP type 5 code 255 (all 0's)\r", 704 i * 4 + 3); 705 } 706 putchar('\n'); 707 } 708 709 if (!ptest || (ptest == 6)) { 710 /* 711 * Type 12, code - 0,127,128,129,255 712 */ 713 icp->icmp_type = 12; 714 icp->icmp_code = 0; 715 (void) send_icmp(nfd, mtu, ip, gwip); 716 PAUSE(); 717 printf("3.6.1 ICMP type 12 code 0 (all 0's)\r"); 718 icp->icmp_code = 127; 719 (void) send_icmp(nfd, mtu, ip, gwip); 720 PAUSE(); 721 printf("3.6.2 ICMP type 12 code 127 (all 0's)\r"); 722 icp->icmp_code = 128; 723 (void) send_icmp(nfd, mtu, ip, gwip); 724 PAUSE(); 725 printf("3.6.3 ICMP type 12 code 128 (all 0's)\r"); 726 icp->icmp_code = 129; 727 (void) send_icmp(nfd, mtu, ip, gwip); 728 PAUSE(); 729 printf("3.6.4 ICMP type 12 code 129 (all 0's)\r"); 730 icp->icmp_code = 255; 731 (void) send_icmp(nfd, mtu, ip, gwip); 732 PAUSE(); 733 printf("3.6.5 ICMP type 12 code 255 (all 0's)\r"); 734 putchar('\n'); 735 } 736 737 if (!ptest || (ptest == 7)) { 738 /* 739 * Type 3;9-10;13-14;17-18 - shorter packets 740 */ 741 ip->ip_len = sizeof(*ip) + sizeof(*icp) / 2; 742 for (i = 0; ict2[i]; i++) { 743 icp->icmp_type = ict1[i]; 744 icp->icmp_code = 0; 745 (void) send_icmp(nfd, mtu, ip, gwip); 746 PAUSE(); 747 printf("3.5.%d ICMP type %d code 0 (all 0's)\r", 748 i * 4, icp->icmp_type); 749 icp->icmp_code = 127; 750 (void) send_icmp(nfd, mtu, ip, gwip); 751 PAUSE(); 752 printf("3.5.%d ICMP type %d code 127 (all 0's)\r", 753 i * 4 + 1, icp->icmp_type); 754 icp->icmp_code = 128; 755 (void) send_icmp(nfd, mtu, ip, gwip); 756 PAUSE(); 757 printf("3.5.%d ICMP type %d code 128 (all 0's)\r", 758 i * 4 + 2, icp->icmp_type); 759 icp->icmp_code = 255; 760 (void) send_icmp(nfd, mtu, ip, gwip); 761 PAUSE(); 762 printf("3.5.%d ICMP type %d code 127 (all 0's)\r", 763 i * 4 + 3, icp->icmp_type); 764 } 765 putchar('\n'); 766 } 767} 768 769 770/* Perform test 4 (UDP) */ 771 772void ip_test4(dev, mtu, ip, gwip, ptest) 773char *dev; 774int mtu; 775ip_t *ip; 776struct in_addr gwip; 777int ptest; 778{ 779#ifdef USE_NANOSLEEP 780 struct timespec ts; 781#else 782 struct timeval tv; 783#endif 784 udphdr_t *u; 785 int nfd, i; 786 787 788 IP_HL_A(ip, sizeof(*ip) >> 2); 789 IP_V_A(ip, IPVERSION); 790 ip->ip_tos = 0; 791 ip->ip_off = 0; 792 ip->ip_ttl = 60; 793 ip->ip_p = IPPROTO_UDP; 794 ip->ip_sum = 0; 795 u = (udphdr_t *)((char *)ip + (IP_HL(ip) << 2)); 796 u->uh_sport = htons(1); 797 u->uh_dport = htons(1); 798 u->uh_ulen = htons(sizeof(*u) + 4); 799 800 nfd = initdevice(dev, 1); 801 if (nfd == -1) 802 return; 803 804 if (!ptest || (ptest == 1)) { 805 /* 806 * Test 1. ulen > packet 807 */ 808 u->uh_ulen = htons(sizeof(*u) + 4); 809 ip->ip_len = (IP_HL(ip) << 2) + ntohs(u->uh_ulen); 810 printf("4.1 UDP uh_ulen > packet size - short packets\n"); 811 for (i = ntohs(u->uh_ulen) * 2; i > sizeof(*u) + 4; i--) { 812 u->uh_ulen = htons(i); 813 (void) send_udp(nfd, 1500, ip, gwip); 814 printf("%d\r", i); 815 fflush(stdout); 816 PAUSE(); 817 } 818 putchar('\n'); 819 } 820 821 if (!ptest || (ptest == 2)) { 822 /* 823 * Test 2. ulen < packet 824 */ 825 u->uh_ulen = htons(sizeof(*u) + 4); 826 ip->ip_len = (IP_HL(ip) << 2) + ntohs(u->uh_ulen); 827 printf("4.2 UDP uh_ulen < packet size - short packets\n"); 828 for (i = ntohs(u->uh_ulen) * 2; i > sizeof(*u) + 4; i--) { 829 ip->ip_len = i; 830 (void) send_udp(nfd, 1500, ip, gwip); 831 printf("%d\r", i); 832 fflush(stdout); 833 PAUSE(); 834 } 835 putchar('\n'); 836 } 837 838 if (!ptest || (ptest == 3)) { 839 /* 840 * Test 3: sport = 0, sport = 1, sport = 32767 841 * sport = 32768, sport = 65535 842 */ 843 u->uh_ulen = sizeof(*u) + 4; 844 ip->ip_len = (IP_HL(ip) << 2) + ntohs(u->uh_ulen); 845 printf("4.3.1 UDP sport = 0\n"); 846 u->uh_sport = 0; 847 (void) send_udp(nfd, 1500, ip, gwip); 848 printf("0\n"); 849 fflush(stdout); 850 PAUSE(); 851 printf("4.3.2 UDP sport = 1\n"); 852 u->uh_sport = htons(1); 853 (void) send_udp(nfd, 1500, ip, gwip); 854 printf("1\n"); 855 fflush(stdout); 856 PAUSE(); 857 printf("4.3.3 UDP sport = 32767\n"); 858 u->uh_sport = htons(32767); 859 (void) send_udp(nfd, 1500, ip, gwip); 860 printf("32767\n"); 861 fflush(stdout); 862 PAUSE(); 863 printf("4.3.4 UDP sport = 32768\n"); 864 u->uh_sport = htons(32768); 865 (void) send_udp(nfd, 1500, ip, gwip); 866 printf("32768\n"); 867 putchar('\n'); 868 fflush(stdout); 869 PAUSE(); 870 printf("4.3.5 UDP sport = 65535\n"); 871 u->uh_sport = htons(65535); 872 (void) send_udp(nfd, 1500, ip, gwip); 873 printf("65535\n"); 874 fflush(stdout); 875 PAUSE(); 876 } 877 878 if (!ptest || (ptest == 4)) { 879 /* 880 * Test 4: dport = 0, dport = 1, dport = 32767 881 * dport = 32768, dport = 65535 882 */ 883 u->uh_ulen = ntohs(sizeof(*u) + 4); 884 u->uh_sport = htons(1); 885 ip->ip_len = (IP_HL(ip) << 2) + ntohs(u->uh_ulen); 886 printf("4.4.1 UDP dport = 0\n"); 887 u->uh_dport = 0; 888 (void) send_udp(nfd, 1500, ip, gwip); 889 printf("0\n"); 890 fflush(stdout); 891 PAUSE(); 892 printf("4.4.2 UDP dport = 1\n"); 893 u->uh_dport = htons(1); 894 (void) send_udp(nfd, 1500, ip, gwip); 895 printf("1\n"); 896 fflush(stdout); 897 PAUSE(); 898 printf("4.4.3 UDP dport = 32767\n"); 899 u->uh_dport = htons(32767); 900 (void) send_udp(nfd, 1500, ip, gwip); 901 printf("32767\n"); 902 fflush(stdout); 903 PAUSE(); 904 printf("4.4.4 UDP dport = 32768\n"); 905 u->uh_dport = htons(32768); 906 (void) send_udp(nfd, 1500, ip, gwip); 907 printf("32768\n"); 908 fflush(stdout); 909 PAUSE(); 910 printf("4.4.5 UDP dport = 65535\n"); 911 u->uh_dport = htons(65535); 912 (void) send_udp(nfd, 1500, ip, gwip); 913 printf("65535\n"); 914 fflush(stdout); 915 PAUSE(); 916 } 917 918 if (!ptest || (ptest == 5)) { 919 /* 920 * Test 5: sizeof(ip_t) <= MTU <= sizeof(udphdr_t) + 921 * sizeof(ip_t) 922 */ 923 printf("4.5 UDP 20 <= MTU <= 32\n"); 924 for (i = sizeof(*ip); i <= ntohs(u->uh_ulen); i++) { 925 (void) send_udp(nfd, i, ip, gwip); 926 printf("%d\r", i); 927 fflush(stdout); 928 PAUSE(); 929 } 930 putchar('\n'); 931 } 932} 933 934 935/* Perform test 5 (TCP) */ 936 937void ip_test5(dev, mtu, ip, gwip, ptest) 938char *dev; 939int mtu; 940ip_t *ip; 941struct in_addr gwip; 942int ptest; 943{ 944#ifdef USE_NANOSLEEP 945 struct timespec ts; 946#else 947 struct timeval tv; 948#endif 949 tcphdr_t *t; 950 int nfd, i; 951 952 t = (tcphdr_t *)((char *)ip + (IP_HL(ip) << 2)); 953#if !defined(linux) && !defined(__osf__) 954 t->th_x2 = 0; 955#endif 956 TCP_OFF_A(t, 0); 957 t->th_sport = htons(1); 958 t->th_dport = htons(1); 959 t->th_win = htons(4096); 960 t->th_urp = 0; 961 t->th_sum = 0; 962 t->th_seq = htonl(1); 963 t->th_ack = 0; 964 ip->ip_len = sizeof(ip_t) + sizeof(tcphdr_t); 965 966 nfd = initdevice(dev, 1); 967 if (nfd == -1) 968 return; 969 970 if (!ptest || (ptest == 1)) { 971 /* 972 * Test 1: flags variations, 0 - 3f 973 */ 974 TCP_OFF_A(t, sizeof(*t) >> 2); 975 printf("5.1 Test TCP flag combinations\n"); 976 for (i = 0; i <= (TH_URG|TH_ACK|TH_PUSH|TH_RST|TH_SYN|TH_FIN); 977 i++) { 978 t->th_flags = i; 979 (void) send_tcp(nfd, mtu, ip, gwip); 980 printf("%d\r", i); 981 fflush(stdout); 982 PAUSE(); 983 } 984 putchar('\n'); 985 } 986 987 if (!ptest || (ptest == 2)) { 988 t->th_flags = TH_SYN; 989 /* 990 * Test 2: seq = 0, seq = 1, seq = 0x7fffffff, seq=0x80000000, 991 * seq = 0xa000000, seq = 0xffffffff 992 */ 993 printf("5.2.1 TCP seq = 0\n"); 994 t->th_seq = htonl(0); 995 (void) send_tcp(nfd, mtu, ip, gwip); 996 fflush(stdout); 997 PAUSE(); 998 999 printf("5.2.2 TCP seq = 1\n"); 1000 t->th_seq = htonl(1); 1001 (void) send_tcp(nfd, mtu, ip, gwip); 1002 fflush(stdout); 1003 PAUSE(); 1004 1005 printf("5.2.3 TCP seq = 0x7fffffff\n"); 1006 t->th_seq = htonl(0x7fffffff); 1007 (void) send_tcp(nfd, mtu, ip, gwip); 1008 fflush(stdout); 1009 PAUSE(); 1010 1011 printf("5.2.4 TCP seq = 0x80000000\n"); 1012 t->th_seq = htonl(0x80000000); 1013 (void) send_tcp(nfd, mtu, ip, gwip); 1014 fflush(stdout); 1015 PAUSE(); 1016 1017 printf("5.2.5 TCP seq = 0xc0000000\n"); 1018 t->th_seq = htonl(0xc0000000); 1019 (void) send_tcp(nfd, mtu, ip, gwip); 1020 fflush(stdout); 1021 PAUSE(); 1022 1023 printf("5.2.6 TCP seq = 0xffffffff\n"); 1024 t->th_seq = htonl(0xffffffff); 1025 (void) send_tcp(nfd, mtu, ip, gwip); 1026 fflush(stdout); 1027 PAUSE(); 1028 } 1029 1030 if (!ptest || (ptest == 3)) { 1031 t->th_flags = TH_ACK; 1032 /* 1033 * Test 3: ack = 0, ack = 1, ack = 0x7fffffff, ack = 0x8000000 1034 * ack = 0xa000000, ack = 0xffffffff 1035 */ 1036 printf("5.3.1 TCP ack = 0\n"); 1037 t->th_ack = 0; 1038 (void) send_tcp(nfd, mtu, ip, gwip); 1039 fflush(stdout); 1040 PAUSE(); 1041 1042 printf("5.3.2 TCP ack = 1\n"); 1043 t->th_ack = htonl(1); 1044 (void) send_tcp(nfd, mtu, ip, gwip); 1045 fflush(stdout); 1046 PAUSE(); 1047 1048 printf("5.3.3 TCP ack = 0x7fffffff\n"); 1049 t->th_ack = htonl(0x7fffffff); 1050 (void) send_tcp(nfd, mtu, ip, gwip); 1051 fflush(stdout); 1052 PAUSE(); 1053 1054 printf("5.3.4 TCP ack = 0x80000000\n"); 1055 t->th_ack = htonl(0x80000000); 1056 (void) send_tcp(nfd, mtu, ip, gwip); 1057 fflush(stdout); 1058 PAUSE(); 1059 1060 printf("5.3.5 TCP ack = 0xc0000000\n"); 1061 t->th_ack = htonl(0xc0000000); 1062 (void) send_tcp(nfd, mtu, ip, gwip); 1063 fflush(stdout); 1064 PAUSE(); 1065 1066 printf("5.3.6 TCP ack = 0xffffffff\n"); 1067 t->th_ack = htonl(0xffffffff); 1068 (void) send_tcp(nfd, mtu, ip, gwip); 1069 fflush(stdout); 1070 PAUSE(); 1071 } 1072 1073 if (!ptest || (ptest == 4)) { 1074 t->th_flags = TH_SYN; 1075 /* 1076 * Test 4: win = 0, win = 32768, win = 65535 1077 */ 1078 printf("5.4.1 TCP win = 0\n"); 1079 t->th_seq = htonl(0); 1080 (void) send_tcp(nfd, mtu, ip, gwip); 1081 fflush(stdout); 1082 PAUSE(); 1083 1084 printf("5.4.2 TCP win = 32768\n"); 1085 t->th_seq = htonl(0x7fff); 1086 (void) send_tcp(nfd, mtu, ip, gwip); 1087 fflush(stdout); 1088 PAUSE(); 1089 1090 printf("5.4.3 TCP win = 65535\n"); 1091 t->th_win = htons(0xffff); 1092 (void) send_tcp(nfd, mtu, ip, gwip); 1093 fflush(stdout); 1094 PAUSE(); 1095 } 1096 1097#if !defined(linux) && !defined(__SVR4) && !defined(__svr4__) && \ 1098 !defined(__sgi) && !defined(__hpux) && !defined(__osf__) 1099 { 1100 struct tcpcb *tcbp, tcb; 1101 struct tcpiphdr ti; 1102 struct sockaddr_in sin; 1103 int fd; 1104 socklen_t slen; 1105 1106 bzero((char *)&sin, sizeof(sin)); 1107 1108 for (i = 1; i < 63; i++) { 1109 fd = socket(AF_INET, SOCK_STREAM, 0); 1110 bzero((char *)&sin, sizeof(sin)); 1111 sin.sin_addr.s_addr = ip->ip_dst.s_addr; 1112 sin.sin_port = htons(i); 1113 sin.sin_family = AF_INET; 1114 if (!connect(fd, (struct sockaddr *)&sin, sizeof(sin))) 1115 break; 1116 close(fd); 1117 } 1118 1119 if (i == 63) { 1120 printf("Couldn't open a TCP socket between ports 1 and 63\n"); 1121 printf("to host %s for test 5 and 6 - skipping.\n", 1122 inet_ntoa(ip->ip_dst)); 1123 goto skip_five_and_six; 1124 } 1125 1126 bcopy((char *)ip, (char *)&ti, sizeof(*ip)); 1127 t->th_dport = htons(i); 1128 slen = sizeof(sin); 1129 if (!getsockname(fd, (struct sockaddr *)&sin, &slen)) 1130 t->th_sport = sin.sin_port; 1131 if (!(tcbp = find_tcp(fd, &ti))) { 1132 printf("Can't find PCB\n"); 1133 goto skip_five_and_six; 1134 } 1135 KMCPY(&tcb, tcbp, sizeof(tcb)); 1136 ti.ti_win = tcb.rcv_adv; 1137 ti.ti_seq = htonl(tcb.snd_nxt - 1); 1138 ti.ti_ack = tcb.rcv_nxt; 1139 1140 if (!ptest || (ptest == 5)) { 1141 /* 1142 * Test 5: urp 1143 */ 1144 t->th_flags = TH_ACK|TH_URG; 1145 printf("5.5.1 TCP Urgent pointer, sport %hu dport %hu\n", 1146 ntohs(t->th_sport), ntohs(t->th_dport)); 1147 t->th_urp = htons(1); 1148 (void) send_tcp(nfd, mtu, ip, gwip); 1149 PAUSE(); 1150 1151 t->th_seq = htonl(tcb.snd_nxt); 1152 ip->ip_len = sizeof(ip_t) + sizeof(tcphdr_t) + 1; 1153 t->th_urp = htons(0x7fff); 1154 (void) send_tcp(nfd, mtu, ip, gwip); 1155 PAUSE(); 1156 t->th_urp = htons(0x8000); 1157 (void) send_tcp(nfd, mtu, ip, gwip); 1158 PAUSE(); 1159 t->th_urp = htons(0xffff); 1160 (void) send_tcp(nfd, mtu, ip, gwip); 1161 PAUSE(); 1162 t->th_urp = 0; 1163 t->th_flags &= ~TH_URG; 1164 ip->ip_len = sizeof(ip_t) + sizeof(tcphdr_t); 1165 } 1166 1167 if (!ptest || (ptest == 6)) { 1168 /* 1169 * Test 6: data offset, off = 0, off is inside, off is outside 1170 */ 1171 t->th_flags = TH_ACK; 1172 printf("5.6.1 TCP off = 1-15, len = 40\n"); 1173 for (i = 1; i < 16; i++) { 1174 TCP_OFF_A(t, ntohs(i)); 1175 (void) send_tcp(nfd, mtu, ip, gwip); 1176 printf("%d\r", i); 1177 fflush(stdout); 1178 PAUSE(); 1179 } 1180 putchar('\n'); 1181 ip->ip_len = sizeof(ip_t) + sizeof(tcphdr_t); 1182 } 1183 1184 (void) close(fd); 1185 } 1186skip_five_and_six: 1187#endif 1188 t->th_seq = htonl(1); 1189 t->th_ack = htonl(1); 1190 TCP_OFF_A(t, 0); 1191 1192 if (!ptest || (ptest == 7)) { 1193 t->th_flags = TH_SYN; 1194 /* 1195 * Test 7: sport = 0, sport = 1, sport = 32767 1196 * sport = 32768, sport = 65535 1197 */ 1198 printf("5.7.1 TCP sport = 0\n"); 1199 t->th_sport = 0; 1200 (void) send_tcp(nfd, mtu, ip, gwip); 1201 fflush(stdout); 1202 PAUSE(); 1203 1204 printf("5.7.2 TCP sport = 1\n"); 1205 t->th_sport = htons(1); 1206 (void) send_tcp(nfd, mtu, ip, gwip); 1207 fflush(stdout); 1208 PAUSE(); 1209 1210 printf("5.7.3 TCP sport = 32767\n"); 1211 t->th_sport = htons(32767); 1212 (void) send_tcp(nfd, mtu, ip, gwip); 1213 fflush(stdout); 1214 PAUSE(); 1215 1216 printf("5.7.4 TCP sport = 32768\n"); 1217 t->th_sport = htons(32768); 1218 (void) send_tcp(nfd, mtu, ip, gwip); 1219 fflush(stdout); 1220 PAUSE(); 1221 1222 printf("5.7.5 TCP sport = 65535\n"); 1223 t->th_sport = htons(65535); 1224 (void) send_tcp(nfd, mtu, ip, gwip); 1225 fflush(stdout); 1226 PAUSE(); 1227 } 1228 1229 if (!ptest || (ptest == 8)) { 1230 t->th_sport = htons(1); 1231 t->th_flags = TH_SYN; 1232 /* 1233 * Test 8: dport = 0, dport = 1, dport = 32767 1234 * dport = 32768, dport = 65535 1235 */ 1236 printf("5.8.1 TCP dport = 0\n"); 1237 t->th_dport = 0; 1238 (void) send_tcp(nfd, mtu, ip, gwip); 1239 fflush(stdout); 1240 PAUSE(); 1241 1242 printf("5.8.2 TCP dport = 1\n"); 1243 t->th_dport = htons(1); 1244 (void) send_tcp(nfd, mtu, ip, gwip); 1245 fflush(stdout); 1246 PAUSE(); 1247 1248 printf("5.8.3 TCP dport = 32767\n"); 1249 t->th_dport = htons(32767); 1250 (void) send_tcp(nfd, mtu, ip, gwip); 1251 fflush(stdout); 1252 PAUSE(); 1253 1254 printf("5.8.4 TCP dport = 32768\n"); 1255 t->th_dport = htons(32768); 1256 (void) send_tcp(nfd, mtu, ip, gwip); 1257 fflush(stdout); 1258 PAUSE(); 1259 1260 printf("5.8.5 TCP dport = 65535\n"); 1261 t->th_dport = htons(65535); 1262 (void) send_tcp(nfd, mtu, ip, gwip); 1263 fflush(stdout); 1264 PAUSE(); 1265 } 1266 1267 /* LAND attack - self connect, so make src & dst ip/port the same */ 1268 if (!ptest || (ptest == 9)) { 1269 printf("5.9 TCP LAND attack. sport = 25, dport = 25\n"); 1270 /* chose SMTP port 25 */ 1271 t->th_sport = htons(25); 1272 t->th_dport = htons(25); 1273 t->th_flags = TH_SYN; 1274 ip->ip_src = ip->ip_dst; 1275 (void) send_tcp(nfd, mtu, ip, gwip); 1276 fflush(stdout); 1277 PAUSE(); 1278 } 1279 1280 /* TCP options header checking */ 1281 /* 0 length options, etc */ 1282} 1283 1284 1285/* Perform test 6 (exhaust mbuf test) */ 1286 1287void ip_test6(dev, mtu, ip, gwip, ptest) 1288char *dev; 1289int mtu; 1290ip_t *ip; 1291struct in_addr gwip; 1292int ptest; 1293{ 1294#ifdef USE_NANOSLEEP 1295 struct timespec ts; 1296#else 1297 struct timeval tv; 1298#endif 1299 udphdr_t *u; 1300 int nfd, i, j, k; 1301 1302 IP_V_A(ip, IPVERSION); 1303 ip->ip_tos = 0; 1304 ip->ip_off = 0; 1305 ip->ip_ttl = 60; 1306 ip->ip_p = IPPROTO_UDP; 1307 ip->ip_sum = 0; 1308 u = (udphdr_t *)(ip + 1); 1309 u->uh_sport = htons(1); 1310 u->uh_dport = htons(9); 1311 u->uh_sum = 0; 1312 1313 nfd = initdevice(dev, 1); 1314 if (nfd == -1) 1315 return; 1316 1317 u->uh_ulen = htons(7168); 1318 1319 printf("6. Exhaustive mbuf test.\n"); 1320 printf(" Send 7k packet in 768 & 128 byte fragments, 128 times.\n"); 1321 printf(" Total of around 8,900 packets\n"); 1322 for (i = 0; i < 128; i++) { 1323 /* 1324 * First send the entire packet in 768 byte chunks. 1325 */ 1326 ip->ip_len = sizeof(*ip) + 768 + sizeof(*u); 1327 IP_HL_A(ip, sizeof(*ip) >> 2); 1328 ip->ip_off = htons(IP_MF); 1329 (void) send_ip(nfd, 1500, ip, gwip, 1); 1330 printf("%d %d\r", i, 0); 1331 fflush(stdout); 1332 PAUSE(); 1333 /* 1334 * And again using 128 byte chunks. 1335 */ 1336 ip->ip_len = sizeof(*ip) + 128 + sizeof(*u); 1337 ip->ip_off = htons(IP_MF); 1338 (void) send_ip(nfd, 1500, ip, gwip, 1); 1339 printf("%d %d\r", i, 0); 1340 fflush(stdout); 1341 PAUSE(); 1342 1343 for (j = 768; j < 3584; j += 768) { 1344 ip->ip_len = sizeof(*ip) + 768; 1345 ip->ip_off = htons(IP_MF|(j>>3)); 1346 (void) send_ip(nfd, 1500, ip, gwip, 1); 1347 printf("%d %d\r", i, j); 1348 fflush(stdout); 1349 PAUSE(); 1350 1351 ip->ip_len = sizeof(*ip) + 128; 1352 for (k = j - 768; k < j; k += 128) { 1353 ip->ip_off = htons(IP_MF|(k>>3)); 1354 (void) send_ip(nfd, 1500, ip, gwip, 1); 1355 printf("%d %d\r", i, k); 1356 fflush(stdout); 1357 PAUSE(); 1358 } 1359 } 1360 } 1361 putchar('\n'); 1362} 1363 1364 1365/* Perform test 7 (random packets) */ 1366 1367static u_long tbuf[64]; 1368 1369void ip_test7(dev, mtu, ip, gwip, ptest) 1370char *dev; 1371int mtu; 1372ip_t *ip; 1373struct in_addr gwip; 1374int ptest; 1375{ 1376 ip_t *pip; 1377#ifdef USE_NANOSLEEP 1378 struct timespec ts; 1379#else 1380 struct timeval tv; 1381#endif 1382 int nfd, i, j; 1383 u_char *s; 1384 1385 nfd = initdevice(dev, 1); 1386 if (nfd == -1) 1387 return; 1388 1389 pip = (ip_t *)tbuf; 1390 1391 srand(time(NULL) ^ (getpid() * getppid())); 1392 1393 printf("7. send 1024 random IP packets.\n"); 1394 1395 for (i = 0; i < 512; i++) { 1396 for (s = (u_char *)pip, j = 0; j < sizeof(tbuf); j++, s++) 1397 *s = (rand() >> 13) & 0xff; 1398 IP_V_A(pip, IPVERSION); 1399 bcopy((char *)&ip->ip_dst, (char *)&pip->ip_dst, 1400 sizeof(struct in_addr)); 1401 pip->ip_sum = 0; 1402 pip->ip_len &= 0xff; 1403 (void) send_ip(nfd, mtu, pip, gwip, 0); 1404 printf("%d\r", i); 1405 fflush(stdout); 1406 PAUSE(); 1407 } 1408 putchar('\n'); 1409 1410 for (i = 0; i < 512; i++) { 1411 for (s = (u_char *)pip, j = 0; j < sizeof(tbuf); j++, s++) 1412 *s = (rand() >> 13) & 0xff; 1413 IP_V_A(pip, IPVERSION); 1414 pip->ip_off &= htons(0xc000); 1415 bcopy((char *)&ip->ip_dst, (char *)&pip->ip_dst, 1416 sizeof(struct in_addr)); 1417 pip->ip_sum = 0; 1418 pip->ip_len &= 0xff; 1419 (void) send_ip(nfd, mtu, pip, gwip, 0); 1420 printf("%d\r", i); 1421 fflush(stdout); 1422 PAUSE(); 1423 } 1424 putchar('\n'); 1425} 1426