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