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