4 * Copyright (C) 1993-1998 by Darren Reed.
6 * See the IPFILTER.LICENCE file for details on licencing.
10 static const char sccsid[] = "%W% %G% (C)1995 Darren Reed";
11 static const char rcsid[] = "@(#)$Id: iptests.c,v 2.8.2.8 2007/02/17 12:41:51 darrenr Exp $";
13 #include <sys/param.h>
14 #include <sys/types.h>
15 #if defined(__NetBSD__) && defined(__vax__)
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.
21 typedef int boolean_t;
27 # if !defined(solaris) && !defined(linux) && !defined(__sgi) && !defined(hpux)
28 # include <sys/file.h>
31 # include <sys/dditypes.h>
37 #if !defined(solaris) && !defined(linux) && !defined(__sgi)
39 # include <sys/user.h>
40 # include <sys/proc.h>
42 #if !defined(ultrix) && !defined(hpux) && !defined(linux) && \
43 !defined(__sgi) && !defined(__osf__) && !defined(_AIX51)
47 # include <sys/socket.h>
50 # include <sys/stream.h>
52 #include <sys/socketvar.h>
54 #include <sys/systm.h>
55 #include <sys/session.h>
58 # include <sys/sysctl.h>
59 # include <sys/filedesc.h>
62 #include <netinet/in_systm.h>
63 #include <sys/socket.h>
65 # define _NET_ROUTE_INCLUDED
68 # include "radix_ipf_local.h"
71 #if defined(linux) && (LINUX >= 0200)
72 # include <asm/atomic.h>
75 # if defined(__FreeBSD__)
76 # include "radix_ipf.h"
78 # include <net/route.h>
80 # define __KERNEL__ /* because there's a macro not wrapped by this */
81 # include <net/route.h> /* in this file :-/ */
83 #include <netinet/in.h>
84 #include <arpa/inet.h>
85 #include <netinet/ip.h>
87 # include <netinet/ip_var.h>
89 # include <netinet/in_pcb.h>
92 #if defined(__SVR4) || defined(__svr4__) || defined(__sgi)
93 # include <sys/sysmacros.h>
100 # undef _NET_ROUTE_INCLUDED
103 #if !defined(linux) && !defined(__hpux)
104 # include <netinet/tcp_timer.h>
105 # include <netinet/tcp_var.h>
107 #if defined(__NetBSD_Version__) && (__NetBSD_Version__ >= 106000000)
108 # define USE_NANOSLEEP
113 # define PAUSE() ts.tv_sec = 0; ts.tv_nsec = 10000000; \
114 (void) nanosleep(&ts, NULL)
116 # define PAUSE() tv.tv_sec = 0; tv.tv_usec = 10000; \
117 (void) select(0, NULL, NULL, NULL, &tv)
121 void ip_test1(dev, mtu, ip, gwip, ptest)
134 int nfd, i = 0, len, id = getpid();
136 IP_HL_A(ip, sizeof(*ip) >> 2);
137 IP_V_A(ip, IPVERSION);
141 ip->ip_p = IPPROTO_UDP;
143 u = (udphdr_t *)(ip + 1);
144 u->uh_sport = htons(1);
145 u->uh_dport = htons(9);
147 u->uh_ulen = htons(sizeof(*u) + 4);
148 ip->ip_len = sizeof(*ip) + ntohs(u->uh_ulen);
151 nfd = initdevice(dev, 1);
155 if (!ptest || (ptest == 1)) {
160 printf("1.1. sending packets with ip_hl < ip_len\n");
161 for (i = 0; i < ((sizeof(*ip) + ntohs(u->uh_ulen)) >> 2); i++) {
163 (void) send_ip(nfd, 1500, ip, gwip, 1);
171 if (!ptest || (ptest == 2)) {
176 printf("1.2. sending packets with ip_hl > ip_len\n");
177 for (; i < ((sizeof(*ip) * 2 + ntohs(u->uh_ulen)) >> 2); i++) {
179 (void) send_ip(nfd, 1500, ip, gwip, 1);
187 if (!ptest || (ptest == 3)) {
192 printf("1.3. ip_v < 4\n");
193 IP_HL_A(ip, sizeof(*ip) >> 2);
194 for (i = 0; i < 4; i++) {
196 (void) send_ip(nfd, 1500, ip, gwip, 1);
204 if (!ptest || (ptest == 4)) {
209 printf("1.4. ip_v > 4\n");
210 for (i = 5; i < 16; i++) {
212 (void) send_ip(nfd, 1500, ip, gwip, 1);
220 if (!ptest || (ptest == 5)) {
222 * Part5: len < packet
225 IP_V_A(ip, IPVERSION);
227 printf("1.5.0 ip_len < packet size (size++, long packets)\n");
228 for (; i < (ip->ip_len * 2); i++) {
229 ip->ip_id = htons(id++);
231 ip->ip_sum = chksum((u_short *)ip, IP_HL(ip) << 2);
232 (void) send_ether(nfd, (char *)ip, i, gwip);
238 printf("1.5.1 ip_len < packet size (ip_len-, short packets)\n");
239 for (i = len; i > 0; i--) {
240 ip->ip_id = htons(id++);
243 ip->ip_sum = chksum((u_short *)ip, IP_HL(ip) << 2);
244 (void) send_ether(nfd, (char *)ip, len, gwip);
252 if (!ptest || (ptest == 6)) {
254 * Part6: len > packet
257 printf("1.6.0 ip_len > packet size (increase ip_len)\n");
258 for (i = len + 1; i < (len * 2); i++) {
259 ip->ip_id = htons(id++);
262 ip->ip_sum = chksum((u_short *)ip, IP_HL(ip) << 2);
263 (void) send_ether(nfd, (char *)ip, len, gwip);
270 printf("1.6.1 ip_len > packet size (size--, short packets)\n");
271 for (i = len; i > 0; i--) {
272 ip->ip_id = htons(id++);
274 ip->ip_sum = chksum((u_short *)ip, IP_HL(ip) << 2);
275 (void) send_ether(nfd, (char *)ip, i, gwip);
283 if (!ptest || (ptest == 7)) {
285 * Part7: 0 length fragment
287 printf("1.7.0 Zero length fragments (ip_off = 0x2000)\n");
289 ip->ip_len = sizeof(*ip);
290 ip->ip_off = htons(IP_MF);
291 (void) send_ip(nfd, mtu, ip, gwip, 1);
295 printf("1.7.1 Zero length fragments (ip_off = 0x3000)\n");
297 ip->ip_len = sizeof(*ip);
298 ip->ip_off = htons(IP_MF);
299 (void) send_ip(nfd, mtu, ip, gwip, 1);
303 printf("1.7.2 Zero length fragments (ip_off = 0xa000)\n");
305 ip->ip_len = sizeof(*ip);
306 ip->ip_off = htons(0xa000);
307 (void) send_ip(nfd, mtu, ip, gwip, 1);
311 printf("1.7.3 Zero length fragments (ip_off = 0x0100)\n");
313 ip->ip_len = sizeof(*ip);
314 ip->ip_off = htons(0x0100);
315 (void) send_ip(nfd, mtu, ip, gwip, 1);
320 if (!ptest || (ptest == 8)) {
323 gettimeofday(&tv, NULL);
324 srand(tv.tv_sec ^ getpid() ^ tv.tv_usec);
326 * Part8.1: 63k packet + 1k fragment at offset 0x1ffe
327 * Mark it as being ICMP (so it doesn't get junked), but
328 * don't bother about the ICMP header, we're not worrying
331 ip->ip_p = IPPROTO_ICMP;
332 ip->ip_off = htons(IP_MF);
333 u->uh_dport = htons(9);
334 ip->ip_id = htons(id++);
335 printf("1.8.1 63k packet + 1k fragment at offset 0x1ffe\n");
336 ip->ip_len = 768 + 20 + 8;
337 (void) send_ip(nfd, mtu, ip, gwip, 1);
340 ip->ip_len = MIN(768 + 20, mtu - 68);
342 for (; i < (63 * 1024 + 768); i += 768) {
343 ip->ip_off = htons(IP_MF | (i >> 3));
344 (void) send_ip(nfd, mtu, ip, gwip, 1);
349 ip->ip_len = 896 + 20;
350 ip->ip_off = htons(i >> 3);
351 (void) send_ip(nfd, mtu, ip, gwip, 1);
357 * Part8.2: 63k packet + 1k fragment at offset 0x1ffe
358 * Mark it as being ICMP (so it doesn't get junked), but
359 * don't bother about the ICMP header, we're not worrying
360 * about that here. (Lossage here)
362 ip->ip_p = IPPROTO_ICMP;
363 ip->ip_off = htons(IP_MF);
364 u->uh_dport = htons(9);
365 ip->ip_id = htons(id++);
366 printf("1.8.2 63k packet + 1k fragment at offset 0x1ffe\n");
367 ip->ip_len = 768 + 20 + 8;
368 if ((rand() & 0x1f) != 0) {
369 (void) send_ip(nfd, mtu, ip, gwip, 1);
374 ip->ip_len = MIN(768 + 20, mtu - 68);
376 for (; i < (63 * 1024 + 768); i += 768) {
377 ip->ip_off = htons(IP_MF | (i >> 3));
378 if ((rand() & 0x1f) != 0) {
379 (void) send_ip(nfd, mtu, ip, gwip, 1);
382 printf("skip %d\n", i);
386 ip->ip_len = 896 + 20;
387 ip->ip_off = htons(i >> 3);
388 if ((rand() & 0x1f) != 0) {
389 (void) send_ip(nfd, mtu, ip, gwip, 1);
397 * Part8.3: 33k packet - test for not dealing with -ve length
398 * Mark it as being ICMP (so it doesn't get junked), but
399 * don't bother about the ICMP header, we're not worrying
402 ip->ip_p = IPPROTO_ICMP;
403 ip->ip_off = htons(IP_MF);
404 u->uh_dport = htons(9);
405 ip->ip_id = htons(id++);
406 printf("1.8.3 33k packet\n");
407 ip->ip_len = 768 + 20 + 8;
408 (void) send_ip(nfd, mtu, ip, gwip, 1);
411 ip->ip_len = MIN(768 + 20, mtu - 68);
413 for (; i < (32 * 1024 + 768); i += 768) {
414 ip->ip_off = htons(IP_MF | (i >> 3));
415 (void) send_ip(nfd, mtu, ip, gwip, 1);
420 ip->ip_len = 896 + 20;
421 ip->ip_off = htons(i >> 3);
422 (void) send_ip(nfd, mtu, ip, gwip, 1);
430 if (!ptest || (ptest == 9)) {
432 * Part9: off & 0x8000 == 0x8000
435 ip->ip_off = htons(0x8000);
436 printf("1.9. ip_off & 0x8000 == 0x8000\n");
437 (void) send_ip(nfd, mtu, ip, gwip, 1);
444 if (!ptest || (ptest == 10)) {
450 printf("1.10.0 ip_ttl = 255\n");
451 (void) send_ip(nfd, mtu, ip, gwip, 1);
456 printf("1.10.1 ip_ttl = 128\n");
457 (void) send_ip(nfd, mtu, ip, gwip, 1);
462 printf("1.10.2 ip_ttl = 0\n");
463 (void) send_ip(nfd, mtu, ip, gwip, 1);
472 void ip_test2(dev, mtu, ip, gwip, ptest)
488 nfd = initdevice(dev, 1);
493 ip->ip_len = IP_HL(ip) << 2;
494 s = (u_char *)(ip + 1);
495 s[IPOPT_OPTVAL] = IPOPT_NOP;
497 if (!ptest || (ptest == 1)) {
499 * Test 1: option length > packet length,
500 * header length == packet length
502 s[IPOPT_OPTVAL] = IPOPT_TS;
504 s[IPOPT_OFFSET] = IPOPT_MINOFF;
505 ip->ip_p = IPPROTO_IP;
506 printf("2.1 option length > packet length\n");
507 (void) send_ip(nfd, mtu, ip, gwip, 1);
513 ip->ip_len = IP_HL(ip) << 2;
514 if (!ptest || (ptest == 1)) {
516 * Test 2: options have length = 0
518 printf("2.2.1 option length = 0, RR\n");
519 s[IPOPT_OPTVAL] = IPOPT_RR;
521 (void) send_ip(nfd, mtu, ip, gwip, 1);
525 printf("2.2.2 option length = 0, TS\n");
526 s[IPOPT_OPTVAL] = IPOPT_TS;
528 (void) send_ip(nfd, mtu, ip, gwip, 1);
532 printf("2.2.3 option length = 0, SECURITY\n");
533 s[IPOPT_OPTVAL] = IPOPT_SECURITY;
535 (void) send_ip(nfd, mtu, ip, gwip, 1);
539 printf("2.2.4 option length = 0, LSRR\n");
540 s[IPOPT_OPTVAL] = IPOPT_LSRR;
542 (void) send_ip(nfd, mtu, ip, gwip, 1);
546 printf("2.2.5 option length = 0, SATID\n");
547 s[IPOPT_OPTVAL] = IPOPT_SATID;
549 (void) send_ip(nfd, mtu, ip, gwip, 1);
553 printf("2.2.6 option length = 0, SSRR\n");
554 s[IPOPT_OPTVAL] = IPOPT_SSRR;
556 (void) send_ip(nfd, mtu, ip, gwip, 1);
568 void ip_test3(dev, mtu, ip, gwip, ptest)
575 static int ict1[10] = { 8, 9, 10, 13, 14, 15, 16, 17, 18, 0 };
576 static int ict2[8] = { 3, 9, 10, 13, 14, 17, 18, 0 };
585 IP_HL_A(ip, sizeof(*ip) >> 2);
586 IP_V_A(ip, IPVERSION);
590 ip->ip_p = IPPROTO_ICMP;
592 ip->ip_len = sizeof(*ip) + sizeof(*icp);
593 icp = (struct icmp *)((char *)ip + (IP_HL(ip) << 2));
595 nfd = initdevice(dev, 1);
599 if (!ptest || (ptest == 1)) {
601 * Type 0 - 31, 255, code = 0
603 bzero((char *)icp, sizeof(*icp));
604 for (i = 0; i < 32; i++) {
606 (void) send_icmp(nfd, mtu, ip, gwip);
608 printf("3.1.%d ICMP type %d code 0 (all 0's)\r", i, i);
610 icp->icmp_type = 255;
611 (void) send_icmp(nfd, mtu, ip, gwip);
613 printf("3.1.%d ICMP type %d code 0 (all 0's)\r", i, 255);
617 if (!ptest || (ptest == 2)) {
619 * Type 3, code = 0 - 31
622 for (i = 0; i < 32; i++) {
624 (void) send_icmp(nfd, mtu, ip, gwip);
626 printf("3.2.%d ICMP type 3 code %d (all 0's)\r", i, i);
630 if (!ptest || (ptest == 3)) {
632 * Type 4, code = 0,127,128,255
636 (void) send_icmp(nfd, mtu, ip, gwip);
638 printf("3.3.1 ICMP type 4 code 0 (all 0's)\r");
639 icp->icmp_code = 127;
640 (void) send_icmp(nfd, mtu, ip, gwip);
642 printf("3.3.2 ICMP type 4 code 127 (all 0's)\r");
643 icp->icmp_code = 128;
644 (void) send_icmp(nfd, mtu, ip, gwip);
646 printf("3.3.3 ICMP type 4 code 128 (all 0's)\r");
647 icp->icmp_code = 255;
648 (void) send_icmp(nfd, mtu, ip, gwip);
650 printf("3.3.4 ICMP type 4 code 255 (all 0's)\r");
653 if (!ptest || (ptest == 4)) {
655 * Type 5, code = 0,127,128,255
659 (void) send_icmp(nfd, mtu, ip, gwip);
661 printf("3.4.1 ICMP type 5 code 0 (all 0's)\r");
662 icp->icmp_code = 127;
663 (void) send_icmp(nfd, mtu, ip, gwip);
665 printf("3.4.2 ICMP type 5 code 127 (all 0's)\r");
666 icp->icmp_code = 128;
667 (void) send_icmp(nfd, mtu, ip, gwip);
669 printf("3.4.3 ICMP type 5 code 128 (all 0's)\r");
670 icp->icmp_code = 255;
671 (void) send_icmp(nfd, mtu, ip, gwip);
673 printf("3.4.4 ICMP type 5 code 255 (all 0's)\r");
676 if (!ptest || (ptest == 5)) {
678 * Type 8-10;13-18, code - 0,127,128,255
680 for (i = 0; ict1[i]; i++) {
681 icp->icmp_type = ict1[i];
683 (void) send_icmp(nfd, mtu, ip, gwip);
685 printf("3.5.%d ICMP type 5 code 0 (all 0's)\r",
687 icp->icmp_code = 127;
688 (void) send_icmp(nfd, mtu, ip, gwip);
690 printf("3.5.%d ICMP type 5 code 127 (all 0's)\r",
692 icp->icmp_code = 128;
693 (void) send_icmp(nfd, mtu, ip, gwip);
695 printf("3.5.%d ICMP type 5 code 128 (all 0's)\r",
697 icp->icmp_code = 255;
698 (void) send_icmp(nfd, mtu, ip, gwip);
700 printf("3.5.%d ICMP type 5 code 255 (all 0's)\r",
706 if (!ptest || (ptest == 6)) {
708 * Type 12, code - 0,127,128,129,255
712 (void) send_icmp(nfd, mtu, ip, gwip);
714 printf("3.6.1 ICMP type 12 code 0 (all 0's)\r");
715 icp->icmp_code = 127;
716 (void) send_icmp(nfd, mtu, ip, gwip);
718 printf("3.6.2 ICMP type 12 code 127 (all 0's)\r");
719 icp->icmp_code = 128;
720 (void) send_icmp(nfd, mtu, ip, gwip);
722 printf("3.6.3 ICMP type 12 code 128 (all 0's)\r");
723 icp->icmp_code = 129;
724 (void) send_icmp(nfd, mtu, ip, gwip);
726 printf("3.6.4 ICMP type 12 code 129 (all 0's)\r");
727 icp->icmp_code = 255;
728 (void) send_icmp(nfd, mtu, ip, gwip);
730 printf("3.6.5 ICMP type 12 code 255 (all 0's)\r");
734 if (!ptest || (ptest == 7)) {
736 * Type 3;9-10;13-14;17-18 - shorter packets
738 ip->ip_len = sizeof(*ip) + sizeof(*icp) / 2;
739 for (i = 0; ict2[i]; i++) {
740 icp->icmp_type = ict1[i];
742 (void) send_icmp(nfd, mtu, ip, gwip);
744 printf("3.5.%d ICMP type %d code 0 (all 0's)\r",
745 i * 4, icp->icmp_type);
746 icp->icmp_code = 127;
747 (void) send_icmp(nfd, mtu, ip, gwip);
749 printf("3.5.%d ICMP type %d code 127 (all 0's)\r",
750 i * 4 + 1, icp->icmp_type);
751 icp->icmp_code = 128;
752 (void) send_icmp(nfd, mtu, ip, gwip);
754 printf("3.5.%d ICMP type %d code 128 (all 0's)\r",
755 i * 4 + 2, icp->icmp_type);
756 icp->icmp_code = 255;
757 (void) send_icmp(nfd, mtu, ip, gwip);
759 printf("3.5.%d ICMP type %d code 127 (all 0's)\r",
760 i * 4 + 3, icp->icmp_type);
767 /* Perform test 4 (UDP) */
769 void ip_test4(dev, mtu, ip, gwip, ptest)
785 IP_HL_A(ip, sizeof(*ip) >> 2);
786 IP_V_A(ip, IPVERSION);
790 ip->ip_p = IPPROTO_UDP;
792 u = (udphdr_t *)((char *)ip + (IP_HL(ip) << 2));
793 u->uh_sport = htons(1);
794 u->uh_dport = htons(1);
795 u->uh_ulen = htons(sizeof(*u) + 4);
797 nfd = initdevice(dev, 1);
801 if (!ptest || (ptest == 1)) {
803 * Test 1. ulen > packet
805 u->uh_ulen = htons(sizeof(*u) + 4);
806 ip->ip_len = (IP_HL(ip) << 2) + ntohs(u->uh_ulen);
807 printf("4.1 UDP uh_ulen > packet size - short packets\n");
808 for (i = ntohs(u->uh_ulen) * 2; i > sizeof(*u) + 4; i--) {
809 u->uh_ulen = htons(i);
810 (void) send_udp(nfd, 1500, ip, gwip);
818 if (!ptest || (ptest == 2)) {
820 * Test 2. ulen < packet
822 u->uh_ulen = htons(sizeof(*u) + 4);
823 ip->ip_len = (IP_HL(ip) << 2) + ntohs(u->uh_ulen);
824 printf("4.2 UDP uh_ulen < packet size - short packets\n");
825 for (i = ntohs(u->uh_ulen) * 2; i > sizeof(*u) + 4; i--) {
827 (void) send_udp(nfd, 1500, ip, gwip);
835 if (!ptest || (ptest == 3)) {
837 * Test 3: sport = 0, sport = 1, sport = 32767
838 * sport = 32768, sport = 65535
840 u->uh_ulen = sizeof(*u) + 4;
841 ip->ip_len = (IP_HL(ip) << 2) + ntohs(u->uh_ulen);
842 printf("4.3.1 UDP sport = 0\n");
844 (void) send_udp(nfd, 1500, ip, gwip);
848 printf("4.3.2 UDP sport = 1\n");
849 u->uh_sport = htons(1);
850 (void) send_udp(nfd, 1500, ip, gwip);
854 printf("4.3.3 UDP sport = 32767\n");
855 u->uh_sport = htons(32767);
856 (void) send_udp(nfd, 1500, ip, gwip);
860 printf("4.3.4 UDP sport = 32768\n");
861 u->uh_sport = htons(32768);
862 (void) send_udp(nfd, 1500, ip, gwip);
867 printf("4.3.5 UDP sport = 65535\n");
868 u->uh_sport = htons(65535);
869 (void) send_udp(nfd, 1500, ip, gwip);
875 if (!ptest || (ptest == 4)) {
877 * Test 4: dport = 0, dport = 1, dport = 32767
878 * dport = 32768, dport = 65535
880 u->uh_ulen = ntohs(sizeof(*u) + 4);
881 u->uh_sport = htons(1);
882 ip->ip_len = (IP_HL(ip) << 2) + ntohs(u->uh_ulen);
883 printf("4.4.1 UDP dport = 0\n");
885 (void) send_udp(nfd, 1500, ip, gwip);
889 printf("4.4.2 UDP dport = 1\n");
890 u->uh_dport = htons(1);
891 (void) send_udp(nfd, 1500, ip, gwip);
895 printf("4.4.3 UDP dport = 32767\n");
896 u->uh_dport = htons(32767);
897 (void) send_udp(nfd, 1500, ip, gwip);
901 printf("4.4.4 UDP dport = 32768\n");
902 u->uh_dport = htons(32768);
903 (void) send_udp(nfd, 1500, ip, gwip);
907 printf("4.4.5 UDP dport = 65535\n");
908 u->uh_dport = htons(65535);
909 (void) send_udp(nfd, 1500, ip, gwip);
915 if (!ptest || (ptest == 5)) {
917 * Test 5: sizeof(ip_t) <= MTU <= sizeof(udphdr_t) +
920 printf("4.5 UDP 20 <= MTU <= 32\n");
921 for (i = sizeof(*ip); i <= ntohs(u->uh_ulen); i++) {
922 (void) send_udp(nfd, i, ip, gwip);
932 /* Perform test 5 (TCP) */
934 void ip_test5(dev, mtu, ip, gwip, ptest)
949 t = (tcphdr_t *)((char *)ip + (IP_HL(ip) << 2));
950 #if !defined(linux) && !defined(__osf__)
954 t->th_sport = htons(1);
955 t->th_dport = htons(1);
956 t->th_win = htons(4096);
959 t->th_seq = htonl(1);
961 ip->ip_len = sizeof(ip_t) + sizeof(tcphdr_t);
963 nfd = initdevice(dev, 1);
967 if (!ptest || (ptest == 1)) {
969 * Test 1: flags variations, 0 - 3f
971 TCP_OFF_A(t, sizeof(*t) >> 2);
972 printf("5.1 Test TCP flag combinations\n");
973 for (i = 0; i <= (TH_URG|TH_ACK|TH_PUSH|TH_RST|TH_SYN|TH_FIN);
976 (void) send_tcp(nfd, mtu, ip, gwip);
984 if (!ptest || (ptest == 2)) {
985 t->th_flags = TH_SYN;
987 * Test 2: seq = 0, seq = 1, seq = 0x7fffffff, seq=0x80000000,
988 * seq = 0xa000000, seq = 0xffffffff
990 printf("5.2.1 TCP seq = 0\n");
991 t->th_seq = htonl(0);
992 (void) send_tcp(nfd, mtu, ip, gwip);
996 printf("5.2.2 TCP seq = 1\n");
997 t->th_seq = htonl(1);
998 (void) send_tcp(nfd, mtu, ip, gwip);
1002 printf("5.2.3 TCP seq = 0x7fffffff\n");
1003 t->th_seq = htonl(0x7fffffff);
1004 (void) send_tcp(nfd, mtu, ip, gwip);
1008 printf("5.2.4 TCP seq = 0x80000000\n");
1009 t->th_seq = htonl(0x80000000);
1010 (void) send_tcp(nfd, mtu, ip, gwip);
1014 printf("5.2.5 TCP seq = 0xc0000000\n");
1015 t->th_seq = htonl(0xc0000000);
1016 (void) send_tcp(nfd, mtu, ip, gwip);
1020 printf("5.2.6 TCP seq = 0xffffffff\n");
1021 t->th_seq = htonl(0xffffffff);
1022 (void) send_tcp(nfd, mtu, ip, gwip);
1027 if (!ptest || (ptest == 3)) {
1028 t->th_flags = TH_ACK;
1030 * Test 3: ack = 0, ack = 1, ack = 0x7fffffff, ack = 0x8000000
1031 * ack = 0xa000000, ack = 0xffffffff
1033 printf("5.3.1 TCP ack = 0\n");
1035 (void) send_tcp(nfd, mtu, ip, gwip);
1039 printf("5.3.2 TCP ack = 1\n");
1040 t->th_ack = htonl(1);
1041 (void) send_tcp(nfd, mtu, ip, gwip);
1045 printf("5.3.3 TCP ack = 0x7fffffff\n");
1046 t->th_ack = htonl(0x7fffffff);
1047 (void) send_tcp(nfd, mtu, ip, gwip);
1051 printf("5.3.4 TCP ack = 0x80000000\n");
1052 t->th_ack = htonl(0x80000000);
1053 (void) send_tcp(nfd, mtu, ip, gwip);
1057 printf("5.3.5 TCP ack = 0xc0000000\n");
1058 t->th_ack = htonl(0xc0000000);
1059 (void) send_tcp(nfd, mtu, ip, gwip);
1063 printf("5.3.6 TCP ack = 0xffffffff\n");
1064 t->th_ack = htonl(0xffffffff);
1065 (void) send_tcp(nfd, mtu, ip, gwip);
1070 if (!ptest || (ptest == 4)) {
1071 t->th_flags = TH_SYN;
1073 * Test 4: win = 0, win = 32768, win = 65535
1075 printf("5.4.1 TCP win = 0\n");
1076 t->th_seq = htonl(0);
1077 (void) send_tcp(nfd, mtu, ip, gwip);
1081 printf("5.4.2 TCP win = 32768\n");
1082 t->th_seq = htonl(0x7fff);
1083 (void) send_tcp(nfd, mtu, ip, gwip);
1087 printf("5.4.3 TCP win = 65535\n");
1088 t->th_win = htons(0xffff);
1089 (void) send_tcp(nfd, mtu, ip, gwip);
1094 #if !defined(linux) && !defined(__SVR4) && !defined(__svr4__) && \
1095 !defined(__sgi) && !defined(__hpux) && !defined(__osf__)
1097 struct tcpcb *tcbp, tcb;
1099 struct sockaddr_in sin;
1102 bzero((char *)&sin, sizeof(sin));
1104 for (i = 1; i < 63; i++) {
1105 fd = socket(AF_INET, SOCK_STREAM, 0);
1106 bzero((char *)&sin, sizeof(sin));
1107 sin.sin_addr.s_addr = ip->ip_dst.s_addr;
1108 sin.sin_port = htons(i);
1109 sin.sin_family = AF_INET;
1110 if (!connect(fd, (struct sockaddr *)&sin, sizeof(sin)))
1116 printf("Couldn't open a TCP socket between ports 1 and 63\n");
1117 printf("to host %s for test 5 and 6 - skipping.\n",
1118 inet_ntoa(ip->ip_dst));
1119 goto skip_five_and_six;
1122 bcopy((char *)ip, (char *)&ti, sizeof(*ip));
1123 t->th_dport = htons(i);
1125 if (!getsockname(fd, (struct sockaddr *)&sin, &slen))
1126 t->th_sport = sin.sin_port;
1127 if (!(tcbp = find_tcp(fd, &ti))) {
1128 printf("Can't find PCB\n");
1129 goto skip_five_and_six;
1131 KMCPY(&tcb, tcbp, sizeof(tcb));
1132 ti.ti_win = tcb.rcv_adv;
1133 ti.ti_seq = htonl(tcb.snd_nxt - 1);
1134 ti.ti_ack = tcb.rcv_nxt;
1136 if (!ptest || (ptest == 5)) {
1140 t->th_flags = TH_ACK|TH_URG;
1141 printf("5.5.1 TCP Urgent pointer, sport %hu dport %hu\n",
1142 ntohs(t->th_sport), ntohs(t->th_dport));
1143 t->th_urp = htons(1);
1144 (void) send_tcp(nfd, mtu, ip, gwip);
1147 t->th_seq = htonl(tcb.snd_nxt);
1148 ip->ip_len = sizeof(ip_t) + sizeof(tcphdr_t) + 1;
1149 t->th_urp = htons(0x7fff);
1150 (void) send_tcp(nfd, mtu, ip, gwip);
1152 t->th_urp = htons(0x8000);
1153 (void) send_tcp(nfd, mtu, ip, gwip);
1155 t->th_urp = htons(0xffff);
1156 (void) send_tcp(nfd, mtu, ip, gwip);
1159 t->th_flags &= ~TH_URG;
1160 ip->ip_len = sizeof(ip_t) + sizeof(tcphdr_t);
1163 if (!ptest || (ptest == 6)) {
1165 * Test 6: data offset, off = 0, off is inside, off is outside
1167 t->th_flags = TH_ACK;
1168 printf("5.6.1 TCP off = 1-15, len = 40\n");
1169 for (i = 1; i < 16; i++) {
1170 TCP_OFF_A(t, ntohs(i));
1171 (void) send_tcp(nfd, mtu, ip, gwip);
1177 ip->ip_len = sizeof(ip_t) + sizeof(tcphdr_t);
1184 t->th_seq = htonl(1);
1185 t->th_ack = htonl(1);
1188 if (!ptest || (ptest == 7)) {
1189 t->th_flags = TH_SYN;
1191 * Test 7: sport = 0, sport = 1, sport = 32767
1192 * sport = 32768, sport = 65535
1194 printf("5.7.1 TCP sport = 0\n");
1196 (void) send_tcp(nfd, mtu, ip, gwip);
1200 printf("5.7.2 TCP sport = 1\n");
1201 t->th_sport = htons(1);
1202 (void) send_tcp(nfd, mtu, ip, gwip);
1206 printf("5.7.3 TCP sport = 32767\n");
1207 t->th_sport = htons(32767);
1208 (void) send_tcp(nfd, mtu, ip, gwip);
1212 printf("5.7.4 TCP sport = 32768\n");
1213 t->th_sport = htons(32768);
1214 (void) send_tcp(nfd, mtu, ip, gwip);
1218 printf("5.7.5 TCP sport = 65535\n");
1219 t->th_sport = htons(65535);
1220 (void) send_tcp(nfd, mtu, ip, gwip);
1225 if (!ptest || (ptest == 8)) {
1226 t->th_sport = htons(1);
1227 t->th_flags = TH_SYN;
1229 * Test 8: dport = 0, dport = 1, dport = 32767
1230 * dport = 32768, dport = 65535
1232 printf("5.8.1 TCP dport = 0\n");
1234 (void) send_tcp(nfd, mtu, ip, gwip);
1238 printf("5.8.2 TCP dport = 1\n");
1239 t->th_dport = htons(1);
1240 (void) send_tcp(nfd, mtu, ip, gwip);
1244 printf("5.8.3 TCP dport = 32767\n");
1245 t->th_dport = htons(32767);
1246 (void) send_tcp(nfd, mtu, ip, gwip);
1250 printf("5.8.4 TCP dport = 32768\n");
1251 t->th_dport = htons(32768);
1252 (void) send_tcp(nfd, mtu, ip, gwip);
1256 printf("5.8.5 TCP dport = 65535\n");
1257 t->th_dport = htons(65535);
1258 (void) send_tcp(nfd, mtu, ip, gwip);
1263 /* LAND attack - self connect, so make src & dst ip/port the same */
1264 if (!ptest || (ptest == 9)) {
1265 printf("5.9 TCP LAND attack. sport = 25, dport = 25\n");
1266 /* chose SMTP port 25 */
1267 t->th_sport = htons(25);
1268 t->th_dport = htons(25);
1269 t->th_flags = TH_SYN;
1270 ip->ip_src = ip->ip_dst;
1271 (void) send_tcp(nfd, mtu, ip, gwip);
1276 /* TCP options header checking */
1277 /* 0 length options, etc */
1281 /* Perform test 6 (exhaust mbuf test) */
1283 void ip_test6(dev, mtu, ip, gwip, ptest)
1287 struct in_addr gwip;
1290 #ifdef USE_NANOSLEEP
1298 IP_V_A(ip, IPVERSION);
1302 ip->ip_p = IPPROTO_UDP;
1304 u = (udphdr_t *)(ip + 1);
1305 u->uh_sport = htons(1);
1306 u->uh_dport = htons(9);
1309 nfd = initdevice(dev, 1);
1313 u->uh_ulen = htons(7168);
1315 printf("6. Exhaustive mbuf test.\n");
1316 printf(" Send 7k packet in 768 & 128 byte fragments, 128 times.\n");
1317 printf(" Total of around 8,900 packets\n");
1318 for (i = 0; i < 128; i++) {
1320 * First send the entire packet in 768 byte chunks.
1322 ip->ip_len = sizeof(*ip) + 768 + sizeof(*u);
1323 IP_HL_A(ip, sizeof(*ip) >> 2);
1324 ip->ip_off = htons(IP_MF);
1325 (void) send_ip(nfd, 1500, ip, gwip, 1);
1326 printf("%d %d\r", i, 0);
1330 * And again using 128 byte chunks.
1332 ip->ip_len = sizeof(*ip) + 128 + sizeof(*u);
1333 ip->ip_off = htons(IP_MF);
1334 (void) send_ip(nfd, 1500, ip, gwip, 1);
1335 printf("%d %d\r", i, 0);
1339 for (j = 768; j < 3584; j += 768) {
1340 ip->ip_len = sizeof(*ip) + 768;
1341 ip->ip_off = htons(IP_MF|(j>>3));
1342 (void) send_ip(nfd, 1500, ip, gwip, 1);
1343 printf("%d %d\r", i, j);
1347 ip->ip_len = sizeof(*ip) + 128;
1348 for (k = j - 768; k < j; k += 128) {
1349 ip->ip_off = htons(IP_MF|(k>>3));
1350 (void) send_ip(nfd, 1500, ip, gwip, 1);
1351 printf("%d %d\r", i, k);
1361 /* Perform test 7 (random packets) */
1363 static u_long tbuf[64];
1365 void ip_test7(dev, mtu, ip, gwip, ptest)
1369 struct in_addr gwip;
1373 #ifdef USE_NANOSLEEP
1381 nfd = initdevice(dev, 1);
1387 srand(time(NULL) ^ (getpid() * getppid()));
1389 printf("7. send 1024 random IP packets.\n");
1391 for (i = 0; i < 512; i++) {
1392 for (s = (u_char *)pip, j = 0; j < sizeof(tbuf); j++, s++)
1393 *s = (rand() >> 13) & 0xff;
1394 IP_V_A(pip, IPVERSION);
1395 bcopy((char *)&ip->ip_dst, (char *)&pip->ip_dst,
1396 sizeof(struct in_addr));
1398 pip->ip_len &= 0xff;
1399 (void) send_ip(nfd, mtu, pip, gwip, 0);
1406 for (i = 0; i < 512; i++) {
1407 for (s = (u_char *)pip, j = 0; j < sizeof(tbuf); j++, s++)
1408 *s = (rand() >> 13) & 0xff;
1409 IP_V_A(pip, IPVERSION);
1410 pip->ip_off &= htons(0xc000);
1411 bcopy((char *)&ip->ip_dst, (char *)&pip->ip_dst,
1412 sizeof(struct in_addr));
1414 pip->ip_len &= 0xff;
1415 (void) send_ip(nfd, mtu, pip, gwip, 0);