2 * Copyright (C) 1993-1998 by Darren Reed.
4 * See the IPFILTER.LICENCE file for details on licencing.
6 #if defined(__sgi) && (IRIX > 602)
7 # include <sys/ptimers.h>
13 #include <sys/types.h>
15 #include <sys/param.h>
18 #if !defined(solaris) && !defined(linux) && !defined(__sgi)
19 # include <sys/file.h>
22 # include <sys/dditypes.h>
27 #if !defined(solaris) && !defined(linux) && !defined(__sgi)
29 # include <sys/user.h>
30 # include <sys/proc.h>
32 #if !defined(ultrix) && !defined(hpux) && !defined(linux) && !defined(__sgi)
36 # include <sys/socket.h>
39 # include <sys/stream.h>
41 #include <sys/socketvar.h>
43 #include <sys/systm.h>
44 #include <sys/session.h>
47 # include <sys/sysctl.h>
48 # include <sys/filedesc.h>
51 #include <netinet/in_systm.h>
52 #include <sys/socket.h>
54 #if defined(linux) && (LINUX >= 0200)
55 # include <asm/atomic.h>
58 # include <net/route.h>
60 # define __KERNEL__ /* because there's a macro not wrapped by this */
61 # include <net/route.h> /* in this file :-/ */
63 #include <netinet/in.h>
64 #include <arpa/inet.h>
65 #include <netinet/ip.h>
66 #include <netinet/tcp.h>
67 #include <netinet/udp.h>
68 #include <netinet/ip_icmp.h>
70 # include <netinet/ip_var.h>
71 # include <netinet/in_pcb.h>
72 # include <netinet/tcp_timer.h>
73 # include <netinet/tcp_var.h>
75 #if defined(__SVR4) || defined(__svr4__) || defined(__sgi)
76 # include <sys/sysmacros.h>
78 #if defined(__NetBSD_Version__) && (__NetBSD_Version__ >= 106000000)
79 # define USE_NANOSLEEP
84 static const char sccsid[] = "%W% %G% (C)1995 Darren Reed";
85 static const char rcsid[] = "@(#)$Id: iptests.c,v 2.1.4.8 2002/12/06 11:40:35 darrenr Exp $";
90 # define PAUSE() ts.tv_sec = 0; ts.tv_nsec = 10000000; \
91 (void) nanosleep(&ts, NULL)
93 # define PAUSE() tv.tv_sec = 0; tv.tv_usec = 10000; \
94 (void) select(0, NULL, NULL, NULL, &tv)
98 void ip_test1(dev, mtu, ip, gwip, ptest)
111 int nfd, i = 0, len, id = getpid();
113 ip->ip_hl = sizeof(*ip) >> 2;
114 ip->ip_v = IPVERSION;
118 ip->ip_p = IPPROTO_UDP;
120 u = (udphdr_t *)(ip + 1);
121 u->uh_sport = htons(1);
122 u->uh_dport = htons(9);
124 u->uh_ulen = htons(sizeof(*u) + 4);
125 ip->ip_len = sizeof(*ip) + ntohs(u->uh_ulen);
127 nfd = initdevice(dev, u->uh_sport, 1);
129 if (!ptest || (ptest == 1)) {
134 printf("1.1. sending packets with ip_hl < ip_len\n");
135 for (i = 0; i < ((sizeof(*ip) + ntohs(u->uh_ulen)) >> 2); i++) {
137 (void) send_ip(nfd, 1500, ip, gwip, 1);
145 if (!ptest || (ptest == 2)) {
150 printf("1.2. sending packets with ip_hl > ip_len\n");
151 for (; i < ((sizeof(*ip) * 2 + ntohs(u->uh_ulen)) >> 2); i++) {
153 (void) send_ip(nfd, 1500, ip, gwip, 1);
161 if (!ptest || (ptest == 3)) {
166 printf("1.3. ip_v < 4\n");
167 ip->ip_hl = sizeof(*ip) >> 2;
168 for (i = 0; i < 4; i++) {
170 (void) send_ip(nfd, 1500, ip, gwip, 1);
178 if (!ptest || (ptest == 4)) {
183 printf("1.4. ip_v > 4\n");
184 for (i = 5; i < 16; i++) {
186 (void) send_ip(nfd, 1500, ip, gwip, 1);
194 if (!ptest || (ptest == 5)) {
196 * Part5: len < packet
199 ip->ip_v = IPVERSION;
201 printf("1.5.0 ip_len < packet size (size++, long packets)\n");
202 for (; i < (ip->ip_len * 2); i++) {
203 ip->ip_id = htons(id++);
205 ip->ip_sum = chksum((u_short *)ip, ip->ip_hl << 2);
206 (void) send_ether(nfd, (char *)ip, i, gwip);
212 printf("1.5.1 ip_len < packet size (ip_len-, short packets)\n");
213 for (i = len; i > 0; i--) {
214 ip->ip_id = htons(id++);
217 ip->ip_sum = chksum((u_short *)ip, ip->ip_hl << 2);
218 (void) send_ether(nfd, (char *)ip, len, gwip);
226 if (!ptest || (ptest == 6)) {
228 * Part6: len > packet
231 printf("1.6.0 ip_len > packet size (increase ip_len)\n");
232 for (i = len + 1; i < (len * 2); i++) {
233 ip->ip_id = htons(id++);
236 ip->ip_sum = chksum((u_short *)ip, ip->ip_hl << 2);
237 (void) send_ether(nfd, (char *)ip, len, gwip);
244 printf("1.6.1 ip_len > packet size (size--, short packets)\n");
245 for (i = len; i > 0; i--) {
246 ip->ip_id = htons(id++);
248 ip->ip_sum = chksum((u_short *)ip, ip->ip_hl << 2);
249 (void) send_ether(nfd, (char *)ip, i, gwip);
257 if (!ptest || (ptest == 7)) {
259 * Part7: 0 length fragment
261 printf("1.7.0 Zero length fragments (ip_off = 0x2000)\n");
263 ip->ip_len = sizeof(*ip);
264 ip->ip_off = htons(IP_MF);
265 (void) send_ip(nfd, mtu, ip, gwip, 1);
269 printf("1.7.1 Zero length fragments (ip_off = 0x3000)\n");
271 ip->ip_len = sizeof(*ip);
272 ip->ip_off = htons(IP_MF);
273 (void) send_ip(nfd, mtu, ip, gwip, 1);
277 printf("1.7.2 Zero length fragments (ip_off = 0xa000)\n");
279 ip->ip_len = sizeof(*ip);
280 ip->ip_off = htons(0xa000);
281 (void) send_ip(nfd, mtu, ip, gwip, 1);
285 printf("1.7.3 Zero length fragments (ip_off = 0x0100)\n");
287 ip->ip_len = sizeof(*ip);
288 ip->ip_off = htons(0x0100);
289 (void) send_ip(nfd, mtu, ip, gwip, 1);
294 if (!ptest || (ptest == 8)) {
297 gettimeofday(&tv, NULL);
298 srand(tv.tv_sec ^ getpid() ^ tv.tv_usec);
300 * Part8.1: 63k packet + 1k fragment at offset 0x1ffe
301 * Mark it as being ICMP (so it doesn't get junked), but
302 * don't bother about the ICMP header, we're not worrying
305 ip->ip_p = IPPROTO_ICMP;
306 ip->ip_off = htons(IP_MF);
307 u->uh_dport = htons(9);
308 ip->ip_id = htons(id++);
309 printf("1.8.1 63k packet + 1k fragment at offset 0x1ffe\n");
310 ip->ip_len = 768 + 20 + 8;
311 (void) send_ip(nfd, mtu, ip, gwip, 1);
314 ip->ip_len = MIN(768 + 20, mtu - 68);
316 for (; i < (63 * 1024 + 768); i += 768) {
317 ip->ip_off = htons(IP_MF | ((i >> 3) & 0x1fff));
318 (void) send_ip(nfd, mtu, ip, gwip, 1);
323 ip->ip_len = 896 + 20;
324 ip->ip_off = htons((i >> 3) & 0x1fff);
325 (void) send_ip(nfd, mtu, ip, gwip, 1);
331 * Part8.2: 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. (Lossage here)
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.2 63k packet + 1k fragment at offset 0x1ffe\n");
341 ip->ip_len = 768 + 20 + 8;
342 if ((rand() & 0x1f) != 0) {
343 (void) send_ip(nfd, mtu, ip, gwip, 1);
348 ip->ip_len = MIN(768 + 20, mtu - 68);
350 for (; i < (63 * 1024 + 768); i += 768) {
351 ip->ip_off = htons(IP_MF | ((i >> 3) & 0x1fff));
352 if ((rand() & 0x1f) != 0) {
353 (void) send_ip(nfd, mtu, ip, gwip, 1);
356 printf("skip %d\n", i);
360 ip->ip_len = 896 + 20;
361 ip->ip_off = htons((i >> 3) & 0x1fff);
362 if ((rand() & 0x1f) != 0) {
363 (void) send_ip(nfd, mtu, ip, gwip, 1);
371 * Part8.3: 33k packet - test for not dealing with -ve length
372 * Mark it as being ICMP (so it doesn't get junked), but
373 * don't bother about the ICMP header, we're not worrying
376 ip->ip_p = IPPROTO_ICMP;
377 ip->ip_off = htons(IP_MF);
378 u->uh_dport = htons(9);
379 ip->ip_id = htons(id++);
380 printf("1.8.3 33k packet\n");
381 ip->ip_len = 768 + 20 + 8;
382 (void) send_ip(nfd, mtu, ip, gwip, 1);
385 ip->ip_len = MIN(768 + 20, mtu - 68);
387 for (; i < (32 * 1024 + 768); i += 768) {
388 ip->ip_off = htons(IP_MF | ((i >> 3) & 0x1fff));
389 (void) send_ip(nfd, mtu, ip, gwip, 1);
394 ip->ip_len = 896 + 20;
395 ip->ip_off = htons((i >> 3) & 0x1fff);
396 (void) send_ip(nfd, mtu, ip, gwip, 1);
404 if (!ptest || (ptest == 9)) {
406 * Part9: off & 0x8000 == 0x8000
409 ip->ip_off = htons(0x8000);
410 printf("1.9. ip_off & 0x8000 == 0x8000\n");
411 (void) send_ip(nfd, mtu, ip, gwip, 1);
418 if (!ptest || (ptest == 10)) {
424 printf("1.10.0 ip_ttl = 255\n");
425 (void) send_ip(nfd, mtu, ip, gwip, 1);
430 printf("1.10.1 ip_ttl = 128\n");
431 (void) send_ip(nfd, mtu, ip, gwip, 1);
436 printf("1.10.2 ip_ttl = 0\n");
437 (void) send_ip(nfd, mtu, ip, gwip, 1);
446 void ip_test2(dev, mtu, ip, gwip, ptest)
461 s = (u_char *)(ip + 1);
462 nfd = initdevice(dev, htons(1), 1);
465 ip->ip_len = ip->ip_hl << 2;
466 s[IPOPT_OPTVAL] = IPOPT_NOP;
468 if (!ptest || (ptest == 1)) {
470 * Test 1: option length > packet length,
471 * header length == packet length
473 s[IPOPT_OPTVAL] = IPOPT_TS;
475 s[IPOPT_OFFSET] = IPOPT_MINOFF;
476 ip->ip_p = IPPROTO_IP;
477 printf("2.1 option length > packet length\n");
478 (void) send_ip(nfd, mtu, ip, gwip, 1);
484 ip->ip_len = ip->ip_hl << 2;
485 if (!ptest || (ptest == 1)) {
487 * Test 2: options have length = 0
489 printf("2.2.1 option length = 0, RR\n");
490 s[IPOPT_OPTVAL] = IPOPT_RR;
492 (void) send_ip(nfd, mtu, ip, gwip, 1);
496 printf("2.2.2 option length = 0, TS\n");
497 s[IPOPT_OPTVAL] = IPOPT_TS;
499 (void) send_ip(nfd, mtu, ip, gwip, 1);
503 printf("2.2.3 option length = 0, SECURITY\n");
504 s[IPOPT_OPTVAL] = IPOPT_SECURITY;
506 (void) send_ip(nfd, mtu, ip, gwip, 1);
510 printf("2.2.4 option length = 0, LSRR\n");
511 s[IPOPT_OPTVAL] = IPOPT_LSRR;
513 (void) send_ip(nfd, mtu, ip, gwip, 1);
517 printf("2.2.5 option length = 0, SATID\n");
518 s[IPOPT_OPTVAL] = IPOPT_SATID;
520 (void) send_ip(nfd, mtu, ip, gwip, 1);
524 printf("2.2.6 option length = 0, SSRR\n");
525 s[IPOPT_OPTVAL] = IPOPT_SSRR;
527 (void) send_ip(nfd, mtu, ip, gwip, 1);
539 void ip_test3(dev, mtu, ip, gwip, ptest)
546 static int ict1[10] = { 8, 9, 10, 13, 14, 15, 16, 17, 18, 0 };
547 static int ict2[8] = { 3, 9, 10, 13, 14, 17, 18, 0 };
556 ip->ip_hl = sizeof(*ip) >> 2;
557 ip->ip_v = IPVERSION;
561 ip->ip_p = IPPROTO_ICMP;
563 ip->ip_len = sizeof(*ip) + sizeof(*icp);
564 icp = (struct icmp *)((char *)ip + (ip->ip_hl << 2));
565 nfd = initdevice(dev, htons(1), 1);
567 if (!ptest || (ptest == 1)) {
569 * Type 0 - 31, 255, code = 0
571 bzero((char *)icp, sizeof(*icp));
572 for (i = 0; i < 32; i++) {
574 (void) send_icmp(nfd, mtu, ip, gwip);
576 printf("3.1.%d ICMP type %d code 0 (all 0's)\r", i, i);
578 icp->icmp_type = 255;
579 (void) send_icmp(nfd, mtu, ip, gwip);
581 printf("3.1.%d ICMP type %d code 0 (all 0's)\r", i, 255);
585 if (!ptest || (ptest == 2)) {
587 * Type 3, code = 0 - 31
590 for (i = 0; i < 32; i++) {
592 (void) send_icmp(nfd, mtu, ip, gwip);
594 printf("3.2.%d ICMP type 3 code %d (all 0's)\r", i, i);
598 if (!ptest || (ptest == 3)) {
600 * Type 4, code = 0,127,128,255
604 (void) send_icmp(nfd, mtu, ip, gwip);
606 printf("3.3.1 ICMP type 4 code 0 (all 0's)\r");
607 icp->icmp_code = 127;
608 (void) send_icmp(nfd, mtu, ip, gwip);
610 printf("3.3.2 ICMP type 4 code 127 (all 0's)\r");
611 icp->icmp_code = 128;
612 (void) send_icmp(nfd, mtu, ip, gwip);
614 printf("3.3.3 ICMP type 4 code 128 (all 0's)\r");
615 icp->icmp_code = 255;
616 (void) send_icmp(nfd, mtu, ip, gwip);
618 printf("3.3.4 ICMP type 4 code 255 (all 0's)\r");
621 if (!ptest || (ptest == 4)) {
623 * Type 5, code = 0,127,128,255
627 (void) send_icmp(nfd, mtu, ip, gwip);
629 printf("3.4.1 ICMP type 5 code 0 (all 0's)\r");
630 icp->icmp_code = 127;
631 (void) send_icmp(nfd, mtu, ip, gwip);
633 printf("3.4.2 ICMP type 5 code 127 (all 0's)\r");
634 icp->icmp_code = 128;
635 (void) send_icmp(nfd, mtu, ip, gwip);
637 printf("3.4.3 ICMP type 5 code 128 (all 0's)\r");
638 icp->icmp_code = 255;
639 (void) send_icmp(nfd, mtu, ip, gwip);
641 printf("3.4.4 ICMP type 5 code 255 (all 0's)\r");
644 if (!ptest || (ptest == 5)) {
646 * Type 8-10;13-18, code - 0,127,128,255
648 for (i = 0; ict1[i]; i++) {
649 icp->icmp_type = ict1[i];
651 (void) send_icmp(nfd, mtu, ip, gwip);
653 printf("3.5.%d ICMP type 5 code 0 (all 0's)\r",
655 icp->icmp_code = 127;
656 (void) send_icmp(nfd, mtu, ip, gwip);
658 printf("3.5.%d ICMP type 5 code 127 (all 0's)\r",
660 icp->icmp_code = 128;
661 (void) send_icmp(nfd, mtu, ip, gwip);
663 printf("3.5.%d ICMP type 5 code 128 (all 0's)\r",
665 icp->icmp_code = 255;
666 (void) send_icmp(nfd, mtu, ip, gwip);
668 printf("3.5.%d ICMP type 5 code 255 (all 0's)\r",
674 if (!ptest || (ptest == 6)) {
676 * Type 12, code - 0,127,128,129,255
680 (void) send_icmp(nfd, mtu, ip, gwip);
682 printf("3.6.1 ICMP type 12 code 0 (all 0's)\r");
683 icp->icmp_code = 127;
684 (void) send_icmp(nfd, mtu, ip, gwip);
686 printf("3.6.2 ICMP type 12 code 127 (all 0's)\r");
687 icp->icmp_code = 128;
688 (void) send_icmp(nfd, mtu, ip, gwip);
690 printf("3.6.3 ICMP type 12 code 128 (all 0's)\r");
691 icp->icmp_code = 129;
692 (void) send_icmp(nfd, mtu, ip, gwip);
694 printf("3.6.4 ICMP type 12 code 129 (all 0's)\r");
695 icp->icmp_code = 255;
696 (void) send_icmp(nfd, mtu, ip, gwip);
698 printf("3.6.5 ICMP type 12 code 255 (all 0's)\r");
702 if (!ptest || (ptest == 7)) {
704 * Type 3;9-10;13-14;17-18 - shorter packets
706 ip->ip_len = sizeof(*ip) + sizeof(*icp) / 2;
707 for (i = 0; ict2[i]; i++) {
708 icp->icmp_type = ict1[i];
710 (void) send_icmp(nfd, mtu, ip, gwip);
712 printf("3.5.%d ICMP type %d code 0 (all 0's)\r",
713 i * 4, icp->icmp_type);
714 icp->icmp_code = 127;
715 (void) send_icmp(nfd, mtu, ip, gwip);
717 printf("3.5.%d ICMP type %d code 127 (all 0's)\r",
718 i * 4 + 1, icp->icmp_type);
719 icp->icmp_code = 128;
720 (void) send_icmp(nfd, mtu, ip, gwip);
722 printf("3.5.%d ICMP type %d code 128 (all 0's)\r",
723 i * 4 + 2, icp->icmp_type);
724 icp->icmp_code = 255;
725 (void) send_icmp(nfd, mtu, ip, gwip);
727 printf("3.5.%d ICMP type %d code 127 (all 0's)\r",
728 i * 4 + 3, icp->icmp_type);
735 /* Perform test 4 (UDP) */
737 void ip_test4(dev, mtu, ip, gwip, ptest)
753 ip->ip_hl = sizeof(*ip) >> 2;
754 ip->ip_v = IPVERSION;
758 ip->ip_p = IPPROTO_UDP;
760 u = (udphdr_t *)((char *)ip + (ip->ip_hl << 2));
761 u->uh_sport = htons(1);
762 u->uh_dport = htons(1);
763 u->uh_ulen = htons(sizeof(*u) + 4);
764 nfd = initdevice(dev, u->uh_sport, 1);
766 if (!ptest || (ptest == 1)) {
768 * Test 1. ulen > packet
770 u->uh_ulen = htons(sizeof(*u) + 4);
771 ip->ip_len = (ip->ip_hl << 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);
783 if (!ptest || (ptest == 2)) {
785 * Test 2. ulen < packet
787 u->uh_ulen = htons(sizeof(*u) + 4);
788 ip->ip_len = (ip->ip_hl << 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--) {
792 (void) send_udp(nfd, 1500, ip, gwip);
800 if (!ptest || (ptest == 3)) {
802 * Test 3: sport = 0, sport = 1, sport = 32767
803 * sport = 32768, sport = 65535
805 u->uh_ulen = sizeof(*u) + 4;
806 ip->ip_len = (ip->ip_hl << 2) + ntohs(u->uh_ulen);
807 printf("4.3.1 UDP sport = 0\n");
809 (void) send_udp(nfd, 1500, ip, gwip);
813 printf("4.3.2 UDP sport = 1\n");
814 u->uh_sport = htons(1);
815 (void) send_udp(nfd, 1500, ip, gwip);
819 printf("4.3.3 UDP sport = 32767\n");
820 u->uh_sport = htons(32767);
821 (void) send_udp(nfd, 1500, ip, gwip);
825 printf("4.3.4 UDP sport = 32768\n");
826 u->uh_sport = htons(32768);
827 (void) send_udp(nfd, 1500, ip, gwip);
832 printf("4.3.5 UDP sport = 65535\n");
833 u->uh_sport = htons(65535);
834 (void) send_udp(nfd, 1500, ip, gwip);
840 if (!ptest || (ptest == 4)) {
842 * Test 4: dport = 0, dport = 1, dport = 32767
843 * dport = 32768, dport = 65535
845 u->uh_ulen = ntohs(sizeof(*u) + 4);
846 u->uh_sport = htons(1);
847 ip->ip_len = (ip->ip_hl << 2) + ntohs(u->uh_ulen);
848 printf("4.4.1 UDP dport = 0\n");
850 (void) send_udp(nfd, 1500, ip, gwip);
854 printf("4.4.2 UDP dport = 1\n");
855 u->uh_dport = htons(1);
856 (void) send_udp(nfd, 1500, ip, gwip);
860 printf("4.4.3 UDP dport = 32767\n");
861 u->uh_dport = htons(32767);
862 (void) send_udp(nfd, 1500, ip, gwip);
866 printf("4.4.4 UDP dport = 32768\n");
867 u->uh_dport = htons(32768);
868 (void) send_udp(nfd, 1500, ip, gwip);
872 printf("4.4.5 UDP dport = 65535\n");
873 u->uh_dport = htons(65535);
874 (void) send_udp(nfd, 1500, ip, gwip);
880 if (!ptest || (ptest == 5)) {
882 * Test 5: sizeof(ip_t) <= MTU <= sizeof(udphdr_t) +
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);
897 /* Perform test 5 (TCP) */
899 void ip_test5(dev, mtu, ip, gwip, ptest)
914 t = (tcphdr_t *)((char *)ip + (ip->ip_hl << 2));
919 t->th_sport = htons(1);
920 t->th_dport = htons(1);
921 t->th_win = htons(4096);
924 t->th_seq = htonl(1);
926 ip->ip_len = sizeof(ip_t) + sizeof(tcphdr_t);
927 nfd = initdevice(dev, t->th_sport, 1);
929 if (!ptest || (ptest == 1)) {
931 * Test 1: flags variations, 0 - 3f
933 t->th_off = sizeof(*t) >> 2;
934 printf("5.1 Test TCP flag combinations\n");
935 for (i = 0; i <= (TH_URG|TH_ACK|TH_PUSH|TH_RST|TH_SYN|TH_FIN);
938 (void) send_tcp(nfd, mtu, ip, gwip);
946 if (!ptest || (ptest == 2)) {
947 t->th_flags = TH_SYN;
949 * Test 2: seq = 0, seq = 1, seq = 0x7fffffff, seq=0x80000000,
950 * seq = 0xa000000, seq = 0xffffffff
952 printf("5.2.1 TCP seq = 0\n");
953 t->th_seq = htonl(0);
954 (void) send_tcp(nfd, mtu, ip, gwip);
958 printf("5.2.2 TCP seq = 1\n");
959 t->th_seq = htonl(1);
960 (void) send_tcp(nfd, mtu, ip, gwip);
964 printf("5.2.3 TCP seq = 0x7fffffff\n");
965 t->th_seq = htonl(0x7fffffff);
966 (void) send_tcp(nfd, mtu, ip, gwip);
970 printf("5.2.4 TCP seq = 0x80000000\n");
971 t->th_seq = htonl(0x80000000);
972 (void) send_tcp(nfd, mtu, ip, gwip);
976 printf("5.2.5 TCP seq = 0xc0000000\n");
977 t->th_seq = htonl(0xc0000000);
978 (void) send_tcp(nfd, mtu, ip, gwip);
982 printf("5.2.6 TCP seq = 0xffffffff\n");
983 t->th_seq = htonl(0xffffffff);
984 (void) send_tcp(nfd, mtu, ip, gwip);
989 if (!ptest || (ptest == 3)) {
990 t->th_flags = TH_ACK;
992 * Test 3: ack = 0, ack = 1, ack = 0x7fffffff, ack = 0x8000000
993 * ack = 0xa000000, ack = 0xffffffff
995 printf("5.3.1 TCP ack = 0\n");
997 (void) send_tcp(nfd, mtu, ip, gwip);
1001 printf("5.3.2 TCP ack = 1\n");
1002 t->th_ack = htonl(1);
1003 (void) send_tcp(nfd, mtu, ip, gwip);
1007 printf("5.3.3 TCP ack = 0x7fffffff\n");
1008 t->th_ack = htonl(0x7fffffff);
1009 (void) send_tcp(nfd, mtu, ip, gwip);
1013 printf("5.3.4 TCP ack = 0x80000000\n");
1014 t->th_ack = htonl(0x80000000);
1015 (void) send_tcp(nfd, mtu, ip, gwip);
1019 printf("5.3.5 TCP ack = 0xc0000000\n");
1020 t->th_ack = htonl(0xc0000000);
1021 (void) send_tcp(nfd, mtu, ip, gwip);
1025 printf("5.3.6 TCP ack = 0xffffffff\n");
1026 t->th_ack = htonl(0xffffffff);
1027 (void) send_tcp(nfd, mtu, ip, gwip);
1032 if (!ptest || (ptest == 4)) {
1033 t->th_flags = TH_SYN;
1035 * Test 4: win = 0, win = 32768, win = 65535
1037 printf("5.4.1 TCP win = 0\n");
1038 t->th_seq = htonl(0);
1039 (void) send_tcp(nfd, mtu, ip, gwip);
1043 printf("5.4.2 TCP win = 32768\n");
1044 t->th_seq = htonl(0x7fff);
1045 (void) send_tcp(nfd, mtu, ip, gwip);
1049 printf("5.4.3 TCP win = 65535\n");
1050 t->th_win = htons(0xffff);
1051 (void) send_tcp(nfd, mtu, ip, gwip);
1056 #if !defined(linux) && !defined(__SVR4) && !defined(__svr4__) && \
1059 struct tcpcb *tcbp, tcb;
1061 struct sockaddr_in sin;
1065 bzero((char *)&sin, sizeof(sin));
1067 for (i = 1; i < 63; i++) {
1068 fd = socket(AF_INET, SOCK_STREAM, 0);
1069 bzero((char *)&sin, sizeof(sin));
1070 sin.sin_addr.s_addr = ip->ip_dst.s_addr;
1071 sin.sin_port = htons(i);
1072 sin.sin_family = AF_INET;
1073 if (!connect(fd, (struct sockaddr *)&sin, sizeof(sin)))
1079 printf("Couldn't open a TCP socket between ports 1 and 63\n");
1080 printf("to host %s for test 5 and 6 - skipping.\n",
1081 inet_ntoa(ip->ip_dst));
1082 goto skip_five_and_six;
1085 bcopy((char *)ip, (char *)&ti, sizeof(*ip));
1086 t->th_dport = htons(i);
1088 if (!getsockname(fd, (struct sockaddr *)&sin, &slen))
1089 t->th_sport = sin.sin_port;
1090 if (!(tcbp = find_tcp(fd, &ti))) {
1091 printf("Can't find PCB\n");
1092 goto skip_five_and_six;
1094 KMCPY(&tcb, tcbp, sizeof(tcb));
1095 ti.ti_win = tcb.rcv_adv;
1096 ti.ti_seq = htonl(tcb.snd_nxt - 1);
1097 ti.ti_ack = tcb.rcv_nxt;
1099 if (!ptest || (ptest == 5)) {
1103 t->th_flags = TH_ACK|TH_URG;
1104 printf("5.5.1 TCP Urgent pointer, sport %hu dport %hu\n",
1105 ntohs(t->th_sport), ntohs(t->th_dport));
1106 t->th_urp = htons(1);
1107 (void) send_tcp(nfd, mtu, ip, gwip);
1110 t->th_seq = htonl(tcb.snd_nxt);
1111 ip->ip_len = sizeof(ip_t) + sizeof(tcphdr_t) + 1;
1112 t->th_urp = htons(0x7fff);
1113 (void) send_tcp(nfd, mtu, ip, gwip);
1115 t->th_urp = htons(0x8000);
1116 (void) send_tcp(nfd, mtu, ip, gwip);
1118 t->th_urp = htons(0xffff);
1119 (void) send_tcp(nfd, mtu, ip, gwip);
1122 t->th_flags &= ~TH_URG;
1123 ip->ip_len = sizeof(ip_t) + sizeof(tcphdr_t);
1126 if (!ptest || (ptest == 6)) {
1128 * Test 6: data offset, off = 0, off is inside, off is outside
1130 t->th_flags = TH_ACK;
1131 printf("5.6.1 TCP off = 1-15, len = 40\n");
1132 for (i = 1; i < 16; i++) {
1133 ti.ti_off = ntohs(i);
1134 (void) send_tcp(nfd, mtu, ip, gwip);
1140 ip->ip_len = sizeof(ip_t) + sizeof(tcphdr_t);
1147 t->th_seq = htonl(1);
1148 t->th_ack = htonl(1);
1151 if (!ptest || (ptest == 7)) {
1152 t->th_flags = TH_SYN;
1154 * Test 7: sport = 0, sport = 1, sport = 32767
1155 * sport = 32768, sport = 65535
1157 printf("5.7.1 TCP sport = 0\n");
1159 (void) send_tcp(nfd, mtu, ip, gwip);
1163 printf("5.7.2 TCP sport = 1\n");
1164 t->th_sport = htons(1);
1165 (void) send_tcp(nfd, mtu, ip, gwip);
1169 printf("5.7.3 TCP sport = 32767\n");
1170 t->th_sport = htons(32767);
1171 (void) send_tcp(nfd, mtu, ip, gwip);
1175 printf("5.7.4 TCP sport = 32768\n");
1176 t->th_sport = htons(32768);
1177 (void) send_tcp(nfd, mtu, ip, gwip);
1181 printf("5.7.5 TCP sport = 65535\n");
1182 t->th_sport = htons(65535);
1183 (void) send_tcp(nfd, mtu, ip, gwip);
1188 if (!ptest || (ptest == 8)) {
1189 t->th_sport = htons(1);
1190 t->th_flags = TH_SYN;
1192 * Test 8: dport = 0, dport = 1, dport = 32767
1193 * dport = 32768, dport = 65535
1195 printf("5.8.1 TCP dport = 0\n");
1197 (void) send_tcp(nfd, mtu, ip, gwip);
1201 printf("5.8.2 TCP dport = 1\n");
1202 t->th_dport = htons(1);
1203 (void) send_tcp(nfd, mtu, ip, gwip);
1207 printf("5.8.3 TCP dport = 32767\n");
1208 t->th_dport = htons(32767);
1209 (void) send_tcp(nfd, mtu, ip, gwip);
1213 printf("5.8.4 TCP dport = 32768\n");
1214 t->th_dport = htons(32768);
1215 (void) send_tcp(nfd, mtu, ip, gwip);
1219 printf("5.8.5 TCP dport = 65535\n");
1220 t->th_dport = htons(65535);
1221 (void) send_tcp(nfd, mtu, ip, gwip);
1226 /* LAND attack - self connect, so make src & dst ip/port the same */
1227 if (!ptest || (ptest == 9)) {
1228 printf("5.9 TCP LAND attack. sport = 25, dport = 25\n");
1229 /* chose SMTP port 25 */
1230 t->th_sport = htons(25);
1231 t->th_dport = htons(25);
1232 t->th_flags = TH_SYN;
1233 ip->ip_src = ip->ip_dst;
1234 (void) send_tcp(nfd, mtu, ip, gwip);
1239 /* TCP options header checking */
1240 /* 0 length options, etc */
1244 /* Perform test 6 (exhaust mbuf test) */
1246 void ip_test6(dev, mtu, ip, gwip, ptest)
1250 struct in_addr gwip;
1253 #ifdef USE_NANOSLEEP
1261 ip->ip_v = IPVERSION;
1265 ip->ip_p = IPPROTO_UDP;
1267 u = (udphdr_t *)(ip + 1);
1268 u->uh_sport = htons(1);
1269 u->uh_dport = htons(9);
1272 nfd = initdevice(dev, u->uh_sport, 1);
1273 u->uh_ulen = htons(7168);
1275 printf("6. Exhaustive mbuf test.\n");
1276 printf(" Send 7k packet in 768 & 128 byte fragments, 128 times.\n");
1277 printf(" Total of around 8,900 packets\n");
1278 for (i = 0; i < 128; i++) {
1280 * First send the entire packet in 768 byte chunks.
1282 ip->ip_len = sizeof(*ip) + 768 + sizeof(*u);
1283 ip->ip_hl = sizeof(*ip) >> 2;
1284 ip->ip_off = htons(IP_MF);
1285 (void) send_ip(nfd, 1500, ip, gwip, 1);
1286 printf("%d %d\r", i, 0);
1290 * And again using 128 byte chunks.
1292 ip->ip_len = sizeof(*ip) + 128 + sizeof(*u);
1293 ip->ip_off = htons(IP_MF);
1294 (void) send_ip(nfd, 1500, ip, gwip, 1);
1295 printf("%d %d\r", i, 0);
1299 for (j = 768; j < 3584; j += 768) {
1300 ip->ip_len = sizeof(*ip) + 768;
1301 ip->ip_off = htons(IP_MF|((j>>3) & 0x1fff));
1302 (void) send_ip(nfd, 1500, ip, gwip, 1);
1303 printf("%d %d\r", i, j);
1307 ip->ip_len = sizeof(*ip) + 128;
1308 for (k = j - 768; k < j; k += 128) {
1309 ip->ip_off = htons(IP_MF|((k>>3) & 0x1fff));
1310 (void) send_ip(nfd, 1500, ip, gwip, 1);
1311 printf("%d %d\r", i, k);
1321 /* Perform test 7 (random packets) */
1323 static u_long tbuf[64];
1325 void ip_test7(dev, mtu, ip, gwip, ptest)
1329 struct in_addr gwip;
1332 #ifdef USE_NANOSLEEP
1341 nfd = initdevice(dev, 0, 1);
1344 srand(time(NULL) ^ (getpid() * getppid()));
1346 printf("7. send 1024 random IP packets.\n");
1348 for (i = 0; i < 512; i++) {
1349 for (s = (u_char *)pip, j = 0; j < sizeof(tbuf); j++, s++)
1350 *s = (rand() >> 13) & 0xff;
1351 pip->ip_v = IPVERSION;
1352 bcopy((char *)&ip->ip_dst, (char *)&pip->ip_dst,
1353 sizeof(struct in_addr));
1355 pip->ip_len &= 0xff;
1356 (void) send_ip(nfd, mtu, pip, gwip, 0);
1363 for (i = 0; i < 512; i++) {
1364 for (s = (u_char *)pip, j = 0; j < sizeof(tbuf); j++, s++)
1365 *s = (rand() >> 13) & 0xff;
1366 pip->ip_v = IPVERSION;
1367 pip->ip_off &= htons(0xc000);
1368 bcopy((char *)&ip->ip_dst, (char *)&pip->ip_dst,
1369 sizeof(struct in_addr));
1371 pip->ip_len &= 0xff;
1372 (void) send_ip(nfd, mtu, pip, gwip, 0);