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