4 * Copyright (C) 2012 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$";
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;
25 # include <machine/lock.h>
26 # include <machine/mutex.h>
30 # if !defined(solaris) && !defined(linux) && !defined(__sgi) && !defined(hpux)
31 # include <sys/file.h>
34 # include <sys/dditypes.h>
39 #if !defined(solaris) && !defined(linux) && !defined(__sgi)
41 # include <sys/user.h>
42 # include <sys/proc.h>
44 #if !defined(ultrix) && !defined(hpux) && !defined(linux) && \
45 !defined(__sgi) && !defined(__osf__) && !defined(_AIX51)
49 # include <sys/socket.h>
52 # include <sys/stream.h>
54 # include <sys/socketvar.h>
57 #include <sys/systm.h>
58 #include <sys/session.h>
61 # include <sys/sysctl.h>
62 # include <sys/filedesc.h>
65 #include <netinet/in_systm.h>
66 #include <sys/socket.h>
68 # if defined(__FreeBSD__)
69 # include "radix_ipf.h"
71 # if !defined(solaris)
72 # include <net/route.h>
74 #include <netinet/in.h>
75 #include <arpa/inet.h>
76 #include <netinet/ip.h>
77 #if defined(__SVR4) || defined(__svr4__) || defined(__sgi)
78 # include <sys/sysmacros.h>
84 # include <netinet/ip_var.h>
85 # if !defined(__hpux) && !defined(solaris)
86 # include <netinet/in_pcb.h>
89 # include <netinet/tcp_timer.h>
90 # include <netinet/tcp_var.h>
91 #if defined(__NetBSD_Version__) && (__NetBSD_Version__ >= 106000000)
92 # define USE_NANOSLEEP
97 # define PAUSE() ts.tv_sec = 0; ts.tv_nsec = 10000000; \
98 (void) nanosleep(&ts, NULL)
100 # define PAUSE() tv.tv_sec = 0; tv.tv_usec = 10000; \
101 (void) select(0, NULL, NULL, NULL, &tv)
105 void ip_test1(dev, mtu, ip, gwip, ptest)
118 int nfd, i = 0, len, id = getpid();
120 IP_HL_A(ip, sizeof(*ip) >> 2);
121 IP_V_A(ip, IPVERSION);
125 ip->ip_p = IPPROTO_UDP;
127 u = (udphdr_t *)(ip + 1);
128 u->uh_sport = htons(1);
129 u->uh_dport = htons(9);
131 u->uh_ulen = htons(sizeof(*u) + 4);
132 ip->ip_len = sizeof(*ip) + ntohs(u->uh_ulen);
135 nfd = initdevice(dev, 1);
139 if (!ptest || (ptest == 1)) {
144 printf("1.1. sending packets with ip_hl < ip_len\n");
145 for (i = 0; i < ((sizeof(*ip) + ntohs(u->uh_ulen)) >> 2); i++) {
147 (void) send_ip(nfd, 1500, ip, gwip, 1);
155 if (!ptest || (ptest == 2)) {
160 printf("1.2. sending packets with ip_hl > ip_len\n");
161 for (; i < ((sizeof(*ip) * 2 + ntohs(u->uh_ulen)) >> 2); i++) {
163 (void) send_ip(nfd, 1500, ip, gwip, 1);
171 if (!ptest || (ptest == 3)) {
176 printf("1.3. ip_v < 4\n");
177 IP_HL_A(ip, sizeof(*ip) >> 2);
178 for (i = 0; i < 4; i++) {
180 (void) send_ip(nfd, 1500, ip, gwip, 1);
188 if (!ptest || (ptest == 4)) {
193 printf("1.4. ip_v > 4\n");
194 for (i = 5; i < 16; i++) {
196 (void) send_ip(nfd, 1500, ip, gwip, 1);
204 if (!ptest || (ptest == 5)) {
206 * Part5: len < packet
209 IP_V_A(ip, IPVERSION);
211 printf("1.5.0 ip_len < packet size (size++, long packets)\n");
212 for (; i < (ip->ip_len * 2); i++) {
213 ip->ip_id = htons(id++);
215 ip->ip_sum = chksum((u_short *)ip, IP_HL(ip) << 2);
216 (void) send_ether(nfd, (char *)ip, i, gwip);
222 printf("1.5.1 ip_len < packet size (ip_len-, short packets)\n");
223 for (i = len; i > 0; i--) {
224 ip->ip_id = htons(id++);
227 ip->ip_sum = chksum((u_short *)ip, IP_HL(ip) << 2);
228 (void) send_ether(nfd, (char *)ip, len, gwip);
236 if (!ptest || (ptest == 6)) {
238 * Part6: len > packet
241 printf("1.6.0 ip_len > packet size (increase ip_len)\n");
242 for (i = len + 1; i < (len * 2); i++) {
243 ip->ip_id = htons(id++);
246 ip->ip_sum = chksum((u_short *)ip, IP_HL(ip) << 2);
247 (void) send_ether(nfd, (char *)ip, len, gwip);
254 printf("1.6.1 ip_len > packet size (size--, short packets)\n");
255 for (i = len; i > 0; i--) {
256 ip->ip_id = htons(id++);
258 ip->ip_sum = chksum((u_short *)ip, IP_HL(ip) << 2);
259 (void) send_ether(nfd, (char *)ip, i, gwip);
267 if (!ptest || (ptest == 7)) {
269 * Part7: 0 length fragment
271 printf("1.7.0 Zero length fragments (ip_off = 0x2000)\n");
273 ip->ip_len = sizeof(*ip);
274 ip->ip_off = htons(IP_MF);
275 (void) send_ip(nfd, mtu, ip, gwip, 1);
279 printf("1.7.1 Zero length fragments (ip_off = 0x3000)\n");
281 ip->ip_len = sizeof(*ip);
282 ip->ip_off = htons(IP_MF);
283 (void) send_ip(nfd, mtu, ip, gwip, 1);
287 printf("1.7.2 Zero length fragments (ip_off = 0xa000)\n");
289 ip->ip_len = sizeof(*ip);
290 ip->ip_off = htons(0xa000);
291 (void) send_ip(nfd, mtu, ip, gwip, 1);
295 printf("1.7.3 Zero length fragments (ip_off = 0x0100)\n");
297 ip->ip_len = sizeof(*ip);
298 ip->ip_off = htons(0x0100);
299 (void) send_ip(nfd, mtu, ip, gwip, 1);
304 if (!ptest || (ptest == 8)) {
307 gettimeofday(&tv, NULL);
308 srand(tv.tv_sec ^ getpid() ^ tv.tv_usec);
310 * Part8.1: 63k packet + 1k fragment at offset 0x1ffe
311 * Mark it as being ICMP (so it doesn't get junked), but
312 * don't bother about the ICMP header, we're not worrying
315 ip->ip_p = IPPROTO_ICMP;
316 ip->ip_off = htons(IP_MF);
317 u->uh_dport = htons(9);
318 ip->ip_id = htons(id++);
319 printf("1.8.1 63k packet + 1k fragment at offset 0x1ffe\n");
320 ip->ip_len = 768 + 20 + 8;
321 (void) send_ip(nfd, mtu, ip, gwip, 1);
324 ip->ip_len = MIN(768 + 20, mtu - 68);
326 for (; i < (63 * 1024 + 768); i += 768) {
327 ip->ip_off = htons(IP_MF | (i >> 3));
328 (void) send_ip(nfd, mtu, ip, gwip, 1);
333 ip->ip_len = 896 + 20;
334 ip->ip_off = htons(i >> 3);
335 (void) send_ip(nfd, mtu, ip, gwip, 1);
341 * Part8.2: 63k packet + 1k fragment at offset 0x1ffe
342 * Mark it as being ICMP (so it doesn't get junked), but
343 * don't bother about the ICMP header, we're not worrying
344 * about that here. (Lossage here)
346 ip->ip_p = IPPROTO_ICMP;
347 ip->ip_off = htons(IP_MF);
348 u->uh_dport = htons(9);
349 ip->ip_id = htons(id++);
350 printf("1.8.2 63k packet + 1k fragment at offset 0x1ffe\n");
351 ip->ip_len = 768 + 20 + 8;
352 if ((rand() & 0x1f) != 0) {
353 (void) send_ip(nfd, mtu, ip, gwip, 1);
358 ip->ip_len = MIN(768 + 20, mtu - 68);
360 for (; i < (63 * 1024 + 768); i += 768) {
361 ip->ip_off = htons(IP_MF | (i >> 3));
362 if ((rand() & 0x1f) != 0) {
363 (void) send_ip(nfd, mtu, ip, gwip, 1);
366 printf("skip %d\n", i);
370 ip->ip_len = 896 + 20;
371 ip->ip_off = htons(i >> 3);
372 if ((rand() & 0x1f) != 0) {
373 (void) send_ip(nfd, mtu, ip, gwip, 1);
381 * Part8.3: 33k packet - test for not dealing with -ve length
382 * Mark it as being ICMP (so it doesn't get junked), but
383 * don't bother about the ICMP header, we're not worrying
386 ip->ip_p = IPPROTO_ICMP;
387 ip->ip_off = htons(IP_MF);
388 u->uh_dport = htons(9);
389 ip->ip_id = htons(id++);
390 printf("1.8.3 33k packet\n");
391 ip->ip_len = 768 + 20 + 8;
392 (void) send_ip(nfd, mtu, ip, gwip, 1);
395 ip->ip_len = MIN(768 + 20, mtu - 68);
397 for (; i < (32 * 1024 + 768); i += 768) {
398 ip->ip_off = htons(IP_MF | (i >> 3));
399 (void) send_ip(nfd, mtu, ip, gwip, 1);
404 ip->ip_len = 896 + 20;
405 ip->ip_off = htons(i >> 3);
406 (void) send_ip(nfd, mtu, ip, gwip, 1);
414 if (!ptest || (ptest == 9)) {
416 * Part9: off & 0x8000 == 0x8000
419 ip->ip_off = htons(0x8000);
420 printf("1.9. ip_off & 0x8000 == 0x8000\n");
421 (void) send_ip(nfd, mtu, ip, gwip, 1);
428 if (!ptest || (ptest == 10)) {
434 printf("1.10.0 ip_ttl = 255\n");
435 (void) send_ip(nfd, mtu, ip, gwip, 1);
440 printf("1.10.1 ip_ttl = 128\n");
441 (void) send_ip(nfd, mtu, ip, gwip, 1);
446 printf("1.10.2 ip_ttl = 0\n");
447 (void) send_ip(nfd, mtu, ip, gwip, 1);
456 void ip_test2(dev, mtu, ip, gwip, ptest)
472 nfd = initdevice(dev, 1);
477 ip->ip_len = IP_HL(ip) << 2;
478 s = (u_char *)(ip + 1);
479 s[IPOPT_OPTVAL] = IPOPT_NOP;
481 if (!ptest || (ptest == 1)) {
483 * Test 1: option length > packet length,
484 * header length == packet length
486 s[IPOPT_OPTVAL] = IPOPT_TS;
488 s[IPOPT_OFFSET] = IPOPT_MINOFF;
489 ip->ip_p = IPPROTO_IP;
490 printf("2.1 option length > packet length\n");
491 (void) send_ip(nfd, mtu, ip, gwip, 1);
497 ip->ip_len = IP_HL(ip) << 2;
498 if (!ptest || (ptest == 1)) {
500 * Test 2: options have length = 0
502 printf("2.2.1 option length = 0, RR\n");
503 s[IPOPT_OPTVAL] = IPOPT_RR;
505 (void) send_ip(nfd, mtu, ip, gwip, 1);
509 printf("2.2.2 option length = 0, TS\n");
510 s[IPOPT_OPTVAL] = IPOPT_TS;
512 (void) send_ip(nfd, mtu, ip, gwip, 1);
516 printf("2.2.3 option length = 0, SECURITY\n");
517 s[IPOPT_OPTVAL] = IPOPT_SECURITY;
519 (void) send_ip(nfd, mtu, ip, gwip, 1);
523 printf("2.2.4 option length = 0, LSRR\n");
524 s[IPOPT_OPTVAL] = IPOPT_LSRR;
526 (void) send_ip(nfd, mtu, ip, gwip, 1);
530 printf("2.2.5 option length = 0, SATID\n");
531 s[IPOPT_OPTVAL] = IPOPT_SATID;
533 (void) send_ip(nfd, mtu, ip, gwip, 1);
537 printf("2.2.6 option length = 0, SSRR\n");
538 s[IPOPT_OPTVAL] = IPOPT_SSRR;
540 (void) send_ip(nfd, mtu, ip, gwip, 1);
552 void ip_test3(dev, mtu, ip, gwip, ptest)
559 static int ict1[10] = { 8, 9, 10, 13, 14, 15, 16, 17, 18, 0 };
560 static int ict2[8] = { 3, 9, 10, 13, 14, 17, 18, 0 };
569 IP_HL_A(ip, sizeof(*ip) >> 2);
570 IP_V_A(ip, IPVERSION);
574 ip->ip_p = IPPROTO_ICMP;
576 ip->ip_len = sizeof(*ip) + sizeof(*icp);
577 icp = (struct icmp *)((char *)ip + (IP_HL(ip) << 2));
579 nfd = initdevice(dev, 1);
583 if (!ptest || (ptest == 1)) {
585 * Type 0 - 31, 255, code = 0
587 bzero((char *)icp, sizeof(*icp));
588 for (i = 0; i < 32; i++) {
590 (void) send_icmp(nfd, mtu, ip, gwip);
592 printf("3.1.%d ICMP type %d code 0 (all 0's)\r", i, i);
594 icp->icmp_type = 255;
595 (void) send_icmp(nfd, mtu, ip, gwip);
597 printf("3.1.%d ICMP type %d code 0 (all 0's)\r", i, 255);
601 if (!ptest || (ptest == 2)) {
603 * Type 3, code = 0 - 31
606 for (i = 0; i < 32; i++) {
608 (void) send_icmp(nfd, mtu, ip, gwip);
610 printf("3.2.%d ICMP type 3 code %d (all 0's)\r", i, i);
614 if (!ptest || (ptest == 3)) {
616 * Type 4, code = 0,127,128,255
620 (void) send_icmp(nfd, mtu, ip, gwip);
622 printf("3.3.1 ICMP type 4 code 0 (all 0's)\r");
623 icp->icmp_code = 127;
624 (void) send_icmp(nfd, mtu, ip, gwip);
626 printf("3.3.2 ICMP type 4 code 127 (all 0's)\r");
627 icp->icmp_code = 128;
628 (void) send_icmp(nfd, mtu, ip, gwip);
630 printf("3.3.3 ICMP type 4 code 128 (all 0's)\r");
631 icp->icmp_code = 255;
632 (void) send_icmp(nfd, mtu, ip, gwip);
634 printf("3.3.4 ICMP type 4 code 255 (all 0's)\r");
637 if (!ptest || (ptest == 4)) {
639 * Type 5, code = 0,127,128,255
643 (void) send_icmp(nfd, mtu, ip, gwip);
645 printf("3.4.1 ICMP type 5 code 0 (all 0's)\r");
646 icp->icmp_code = 127;
647 (void) send_icmp(nfd, mtu, ip, gwip);
649 printf("3.4.2 ICMP type 5 code 127 (all 0's)\r");
650 icp->icmp_code = 128;
651 (void) send_icmp(nfd, mtu, ip, gwip);
653 printf("3.4.3 ICMP type 5 code 128 (all 0's)\r");
654 icp->icmp_code = 255;
655 (void) send_icmp(nfd, mtu, ip, gwip);
657 printf("3.4.4 ICMP type 5 code 255 (all 0's)\r");
660 if (!ptest || (ptest == 5)) {
662 * Type 8-10;13-18, code - 0,127,128,255
664 for (i = 0; ict1[i]; i++) {
665 icp->icmp_type = ict1[i];
667 (void) send_icmp(nfd, mtu, ip, gwip);
669 printf("3.5.%d ICMP type 5 code 0 (all 0's)\r",
671 icp->icmp_code = 127;
672 (void) send_icmp(nfd, mtu, ip, gwip);
674 printf("3.5.%d ICMP type 5 code 127 (all 0's)\r",
676 icp->icmp_code = 128;
677 (void) send_icmp(nfd, mtu, ip, gwip);
679 printf("3.5.%d ICMP type 5 code 128 (all 0's)\r",
681 icp->icmp_code = 255;
682 (void) send_icmp(nfd, mtu, ip, gwip);
684 printf("3.5.%d ICMP type 5 code 255 (all 0's)\r",
690 if (!ptest || (ptest == 6)) {
692 * Type 12, code - 0,127,128,129,255
696 (void) send_icmp(nfd, mtu, ip, gwip);
698 printf("3.6.1 ICMP type 12 code 0 (all 0's)\r");
699 icp->icmp_code = 127;
700 (void) send_icmp(nfd, mtu, ip, gwip);
702 printf("3.6.2 ICMP type 12 code 127 (all 0's)\r");
703 icp->icmp_code = 128;
704 (void) send_icmp(nfd, mtu, ip, gwip);
706 printf("3.6.3 ICMP type 12 code 128 (all 0's)\r");
707 icp->icmp_code = 129;
708 (void) send_icmp(nfd, mtu, ip, gwip);
710 printf("3.6.4 ICMP type 12 code 129 (all 0's)\r");
711 icp->icmp_code = 255;
712 (void) send_icmp(nfd, mtu, ip, gwip);
714 printf("3.6.5 ICMP type 12 code 255 (all 0's)\r");
718 if (!ptest || (ptest == 7)) {
720 * Type 3;9-10;13-14;17-18 - shorter packets
722 ip->ip_len = sizeof(*ip) + sizeof(*icp) / 2;
723 for (i = 0; ict2[i]; i++) {
724 icp->icmp_type = ict1[i];
726 (void) send_icmp(nfd, mtu, ip, gwip);
728 printf("3.5.%d ICMP type %d code 0 (all 0's)\r",
729 i * 4, icp->icmp_type);
730 icp->icmp_code = 127;
731 (void) send_icmp(nfd, mtu, ip, gwip);
733 printf("3.5.%d ICMP type %d code 127 (all 0's)\r",
734 i * 4 + 1, icp->icmp_type);
735 icp->icmp_code = 128;
736 (void) send_icmp(nfd, mtu, ip, gwip);
738 printf("3.5.%d ICMP type %d code 128 (all 0's)\r",
739 i * 4 + 2, icp->icmp_type);
740 icp->icmp_code = 255;
741 (void) send_icmp(nfd, mtu, ip, gwip);
743 printf("3.5.%d ICMP type %d code 127 (all 0's)\r",
744 i * 4 + 3, icp->icmp_type);
751 /* Perform test 4 (UDP) */
753 void ip_test4(dev, mtu, ip, gwip, ptest)
769 IP_HL_A(ip, sizeof(*ip) >> 2);
770 IP_V_A(ip, IPVERSION);
774 ip->ip_p = IPPROTO_UDP;
776 u = (udphdr_t *)((char *)ip + (IP_HL(ip) << 2));
777 u->uh_sport = htons(1);
778 u->uh_dport = htons(1);
779 u->uh_ulen = htons(sizeof(*u) + 4);
781 nfd = initdevice(dev, 1);
785 if (!ptest || (ptest == 1)) {
787 * Test 1. ulen > packet
789 u->uh_ulen = htons(sizeof(*u) + 4);
790 ip->ip_len = (IP_HL(ip) << 2) + ntohs(u->uh_ulen);
791 printf("4.1 UDP uh_ulen > packet size - short packets\n");
792 for (i = ntohs(u->uh_ulen) * 2; i > sizeof(*u) + 4; i--) {
793 u->uh_ulen = htons(i);
794 (void) send_udp(nfd, 1500, ip, gwip);
802 if (!ptest || (ptest == 2)) {
804 * Test 2. ulen < packet
806 u->uh_ulen = htons(sizeof(*u) + 4);
807 ip->ip_len = (IP_HL(ip) << 2) + ntohs(u->uh_ulen);
808 printf("4.2 UDP uh_ulen < packet size - short packets\n");
809 for (i = ntohs(u->uh_ulen) * 2; i > sizeof(*u) + 4; i--) {
811 (void) send_udp(nfd, 1500, ip, gwip);
819 if (!ptest || (ptest == 3)) {
821 * Test 3: sport = 0, sport = 1, sport = 32767
822 * sport = 32768, sport = 65535
824 u->uh_ulen = sizeof(*u) + 4;
825 ip->ip_len = (IP_HL(ip) << 2) + ntohs(u->uh_ulen);
826 printf("4.3.1 UDP sport = 0\n");
828 (void) send_udp(nfd, 1500, ip, gwip);
832 printf("4.3.2 UDP sport = 1\n");
833 u->uh_sport = htons(1);
834 (void) send_udp(nfd, 1500, ip, gwip);
838 printf("4.3.3 UDP sport = 32767\n");
839 u->uh_sport = htons(32767);
840 (void) send_udp(nfd, 1500, ip, gwip);
844 printf("4.3.4 UDP sport = 32768\n");
845 u->uh_sport = htons(32768);
846 (void) send_udp(nfd, 1500, ip, gwip);
851 printf("4.3.5 UDP sport = 65535\n");
852 u->uh_sport = htons(65535);
853 (void) send_udp(nfd, 1500, ip, gwip);
859 if (!ptest || (ptest == 4)) {
861 * Test 4: dport = 0, dport = 1, dport = 32767
862 * dport = 32768, dport = 65535
864 u->uh_ulen = ntohs(sizeof(*u) + 4);
865 u->uh_sport = htons(1);
866 ip->ip_len = (IP_HL(ip) << 2) + ntohs(u->uh_ulen);
867 printf("4.4.1 UDP dport = 0\n");
869 (void) send_udp(nfd, 1500, ip, gwip);
873 printf("4.4.2 UDP dport = 1\n");
874 u->uh_dport = htons(1);
875 (void) send_udp(nfd, 1500, ip, gwip);
879 printf("4.4.3 UDP dport = 32767\n");
880 u->uh_dport = htons(32767);
881 (void) send_udp(nfd, 1500, ip, gwip);
885 printf("4.4.4 UDP dport = 32768\n");
886 u->uh_dport = htons(32768);
887 (void) send_udp(nfd, 1500, ip, gwip);
891 printf("4.4.5 UDP dport = 65535\n");
892 u->uh_dport = htons(65535);
893 (void) send_udp(nfd, 1500, ip, gwip);
899 if (!ptest || (ptest == 5)) {
901 * Test 5: sizeof(ip_t) <= MTU <= sizeof(udphdr_t) +
904 printf("4.5 UDP 20 <= MTU <= 32\n");
905 for (i = sizeof(*ip); i <= ntohs(u->uh_ulen); i++) {
906 (void) send_udp(nfd, i, ip, gwip);
916 /* Perform test 5 (TCP) */
918 void ip_test5(dev, mtu, ip, gwip, ptest)
933 t = (tcphdr_t *)((char *)ip + (IP_HL(ip) << 2));
936 t->th_sport = htons(1);
937 t->th_dport = htons(1);
938 t->th_win = htons(4096);
941 t->th_seq = htonl(1);
943 ip->ip_len = sizeof(ip_t) + sizeof(tcphdr_t);
945 nfd = initdevice(dev, 1);
949 if (!ptest || (ptest == 1)) {
951 * Test 1: flags variations, 0 - 3f
953 TCP_OFF_A(t, sizeof(*t) >> 2);
954 printf("5.1 Test TCP flag combinations\n");
955 for (i = 0; i <= (TH_URG|TH_ACK|TH_PUSH|TH_RST|TH_SYN|TH_FIN);
958 (void) send_tcp(nfd, mtu, ip, gwip);
966 if (!ptest || (ptest == 2)) {
967 t->th_flags = TH_SYN;
969 * Test 2: seq = 0, seq = 1, seq = 0x7fffffff, seq=0x80000000,
970 * seq = 0xa000000, seq = 0xffffffff
972 printf("5.2.1 TCP seq = 0\n");
973 t->th_seq = htonl(0);
974 (void) send_tcp(nfd, mtu, ip, gwip);
978 printf("5.2.2 TCP seq = 1\n");
979 t->th_seq = htonl(1);
980 (void) send_tcp(nfd, mtu, ip, gwip);
984 printf("5.2.3 TCP seq = 0x7fffffff\n");
985 t->th_seq = htonl(0x7fffffff);
986 (void) send_tcp(nfd, mtu, ip, gwip);
990 printf("5.2.4 TCP seq = 0x80000000\n");
991 t->th_seq = htonl(0x80000000);
992 (void) send_tcp(nfd, mtu, ip, gwip);
996 printf("5.2.5 TCP seq = 0xc0000000\n");
997 t->th_seq = htonl(0xc0000000);
998 (void) send_tcp(nfd, mtu, ip, gwip);
1002 printf("5.2.6 TCP seq = 0xffffffff\n");
1003 t->th_seq = htonl(0xffffffff);
1004 (void) send_tcp(nfd, mtu, ip, gwip);
1009 if (!ptest || (ptest == 3)) {
1010 t->th_flags = TH_ACK;
1012 * Test 3: ack = 0, ack = 1, ack = 0x7fffffff, ack = 0x8000000
1013 * ack = 0xa000000, ack = 0xffffffff
1015 printf("5.3.1 TCP ack = 0\n");
1017 (void) send_tcp(nfd, mtu, ip, gwip);
1021 printf("5.3.2 TCP ack = 1\n");
1022 t->th_ack = htonl(1);
1023 (void) send_tcp(nfd, mtu, ip, gwip);
1027 printf("5.3.3 TCP ack = 0x7fffffff\n");
1028 t->th_ack = htonl(0x7fffffff);
1029 (void) send_tcp(nfd, mtu, ip, gwip);
1033 printf("5.3.4 TCP ack = 0x80000000\n");
1034 t->th_ack = htonl(0x80000000);
1035 (void) send_tcp(nfd, mtu, ip, gwip);
1039 printf("5.3.5 TCP ack = 0xc0000000\n");
1040 t->th_ack = htonl(0xc0000000);
1041 (void) send_tcp(nfd, mtu, ip, gwip);
1045 printf("5.3.6 TCP ack = 0xffffffff\n");
1046 t->th_ack = htonl(0xffffffff);
1047 (void) send_tcp(nfd, mtu, ip, gwip);
1052 if (!ptest || (ptest == 4)) {
1053 t->th_flags = TH_SYN;
1055 * Test 4: win = 0, win = 32768, win = 65535
1057 printf("5.4.1 TCP win = 0\n");
1058 t->th_seq = htonl(0);
1059 (void) send_tcp(nfd, mtu, ip, gwip);
1063 printf("5.4.2 TCP win = 32768\n");
1064 t->th_seq = htonl(0x7fff);
1065 (void) send_tcp(nfd, mtu, ip, gwip);
1069 printf("5.4.3 TCP win = 65535\n");
1070 t->th_win = htons(0xffff);
1071 (void) send_tcp(nfd, mtu, ip, gwip);
1076 #if !defined(linux) && !defined(__SVR4) && !defined(__svr4__) && \
1077 !defined(__sgi) && !defined(__hpux) && !defined(__osf__)
1079 struct tcpcb *tcbp, tcb;
1081 struct sockaddr_in sin;
1085 bzero((char *)&sin, sizeof(sin));
1087 for (i = 1; i < 63; i++) {
1088 fd = socket(AF_INET, SOCK_STREAM, 0);
1089 bzero((char *)&sin, sizeof(sin));
1090 sin.sin_addr.s_addr = ip->ip_dst.s_addr;
1091 sin.sin_port = htons(i);
1092 sin.sin_family = AF_INET;
1093 if (!connect(fd, (struct sockaddr *)&sin, sizeof(sin)))
1099 printf("Couldn't open a TCP socket between ports 1 and 63\n");
1100 printf("to host %s for test 5 and 6 - skipping.\n",
1101 inet_ntoa(ip->ip_dst));
1102 goto skip_five_and_six;
1105 bcopy((char *)ip, (char *)&ti, sizeof(*ip));
1106 t->th_dport = htons(i);
1108 if (!getsockname(fd, (struct sockaddr *)&sin, &slen))
1109 t->th_sport = sin.sin_port;
1110 if (!(tcbp = find_tcp(fd, &ti))) {
1111 printf("Can't find PCB\n");
1112 goto skip_five_and_six;
1114 KMCPY(&tcb, tcbp, sizeof(tcb));
1115 ti.ti_win = tcb.rcv_adv;
1116 ti.ti_seq = htonl(tcb.snd_nxt - 1);
1117 ti.ti_ack = tcb.rcv_nxt;
1119 if (!ptest || (ptest == 5)) {
1123 t->th_flags = TH_ACK|TH_URG;
1124 printf("5.5.1 TCP Urgent pointer, sport %hu dport %hu\n",
1125 ntohs(t->th_sport), ntohs(t->th_dport));
1126 t->th_urp = htons(1);
1127 (void) send_tcp(nfd, mtu, ip, gwip);
1130 t->th_seq = htonl(tcb.snd_nxt);
1131 ip->ip_len = sizeof(ip_t) + sizeof(tcphdr_t) + 1;
1132 t->th_urp = htons(0x7fff);
1133 (void) send_tcp(nfd, mtu, ip, gwip);
1135 t->th_urp = htons(0x8000);
1136 (void) send_tcp(nfd, mtu, ip, gwip);
1138 t->th_urp = htons(0xffff);
1139 (void) send_tcp(nfd, mtu, ip, gwip);
1142 t->th_flags &= ~TH_URG;
1143 ip->ip_len = sizeof(ip_t) + sizeof(tcphdr_t);
1146 if (!ptest || (ptest == 6)) {
1148 * Test 6: data offset, off = 0, off is inside, off is outside
1150 t->th_flags = TH_ACK;
1151 printf("5.6.1 TCP off = 1-15, len = 40\n");
1152 for (i = 1; i < 16; i++) {
1153 TCP_OFF_A(t, ntohs(i));
1154 (void) send_tcp(nfd, mtu, ip, gwip);
1160 ip->ip_len = sizeof(ip_t) + sizeof(tcphdr_t);
1167 t->th_seq = htonl(1);
1168 t->th_ack = htonl(1);
1171 if (!ptest || (ptest == 7)) {
1172 t->th_flags = TH_SYN;
1174 * Test 7: sport = 0, sport = 1, sport = 32767
1175 * sport = 32768, sport = 65535
1177 printf("5.7.1 TCP sport = 0\n");
1179 (void) send_tcp(nfd, mtu, ip, gwip);
1183 printf("5.7.2 TCP sport = 1\n");
1184 t->th_sport = htons(1);
1185 (void) send_tcp(nfd, mtu, ip, gwip);
1189 printf("5.7.3 TCP sport = 32767\n");
1190 t->th_sport = htons(32767);
1191 (void) send_tcp(nfd, mtu, ip, gwip);
1195 printf("5.7.4 TCP sport = 32768\n");
1196 t->th_sport = htons(32768);
1197 (void) send_tcp(nfd, mtu, ip, gwip);
1201 printf("5.7.5 TCP sport = 65535\n");
1202 t->th_sport = htons(65535);
1203 (void) send_tcp(nfd, mtu, ip, gwip);
1208 if (!ptest || (ptest == 8)) {
1209 t->th_sport = htons(1);
1210 t->th_flags = TH_SYN;
1212 * Test 8: dport = 0, dport = 1, dport = 32767
1213 * dport = 32768, dport = 65535
1215 printf("5.8.1 TCP dport = 0\n");
1217 (void) send_tcp(nfd, mtu, ip, gwip);
1221 printf("5.8.2 TCP dport = 1\n");
1222 t->th_dport = htons(1);
1223 (void) send_tcp(nfd, mtu, ip, gwip);
1227 printf("5.8.3 TCP dport = 32767\n");
1228 t->th_dport = htons(32767);
1229 (void) send_tcp(nfd, mtu, ip, gwip);
1233 printf("5.8.4 TCP dport = 32768\n");
1234 t->th_dport = htons(32768);
1235 (void) send_tcp(nfd, mtu, ip, gwip);
1239 printf("5.8.5 TCP dport = 65535\n");
1240 t->th_dport = htons(65535);
1241 (void) send_tcp(nfd, mtu, ip, gwip);
1246 /* LAND attack - self connect, so make src & dst ip/port the same */
1247 if (!ptest || (ptest == 9)) {
1248 printf("5.9 TCP LAND attack. sport = 25, dport = 25\n");
1249 /* chose SMTP port 25 */
1250 t->th_sport = htons(25);
1251 t->th_dport = htons(25);
1252 t->th_flags = TH_SYN;
1253 ip->ip_src = ip->ip_dst;
1254 (void) send_tcp(nfd, mtu, ip, gwip);
1259 /* TCP options header checking */
1260 /* 0 length options, etc */
1264 /* Perform test 6 (exhaust mbuf test) */
1266 void ip_test6(dev, mtu, ip, gwip, ptest)
1270 struct in_addr gwip;
1273 #ifdef USE_NANOSLEEP
1281 IP_V_A(ip, IPVERSION);
1285 ip->ip_p = IPPROTO_UDP;
1287 u = (udphdr_t *)(ip + 1);
1288 u->uh_sport = htons(1);
1289 u->uh_dport = htons(9);
1292 nfd = initdevice(dev, 1);
1296 u->uh_ulen = htons(7168);
1298 printf("6. Exhaustive mbuf test.\n");
1299 printf(" Send 7k packet in 768 & 128 byte fragments, 128 times.\n");
1300 printf(" Total of around 8,900 packets\n");
1301 for (i = 0; i < 128; i++) {
1303 * First send the entire packet in 768 byte chunks.
1305 ip->ip_len = sizeof(*ip) + 768 + sizeof(*u);
1306 IP_HL_A(ip, sizeof(*ip) >> 2);
1307 ip->ip_off = htons(IP_MF);
1308 (void) send_ip(nfd, 1500, ip, gwip, 1);
1309 printf("%d %d\r", i, 0);
1313 * And again using 128 byte chunks.
1315 ip->ip_len = sizeof(*ip) + 128 + sizeof(*u);
1316 ip->ip_off = htons(IP_MF);
1317 (void) send_ip(nfd, 1500, ip, gwip, 1);
1318 printf("%d %d\r", i, 0);
1322 for (j = 768; j < 3584; j += 768) {
1323 ip->ip_len = sizeof(*ip) + 768;
1324 ip->ip_off = htons(IP_MF|(j>>3));
1325 (void) send_ip(nfd, 1500, ip, gwip, 1);
1326 printf("%d %d\r", i, j);
1330 ip->ip_len = sizeof(*ip) + 128;
1331 for (k = j - 768; k < j; k += 128) {
1332 ip->ip_off = htons(IP_MF|(k>>3));
1333 (void) send_ip(nfd, 1500, ip, gwip, 1);
1334 printf("%d %d\r", i, k);
1344 /* Perform test 7 (random packets) */
1346 static u_long tbuf[64];
1348 void ip_test7(dev, mtu, ip, gwip, ptest)
1352 struct in_addr gwip;
1356 #ifdef USE_NANOSLEEP
1364 nfd = initdevice(dev, 1);
1370 srand(time(NULL) ^ (getpid() * getppid()));
1372 printf("7. send 1024 random IP packets.\n");
1374 for (i = 0; i < 512; i++) {
1375 for (s = (u_char *)pip, j = 0; j < sizeof(tbuf); j++, s++)
1376 *s = (rand() >> 13) & 0xff;
1377 IP_V_A(pip, IPVERSION);
1378 bcopy((char *)&ip->ip_dst, (char *)&pip->ip_dst,
1379 sizeof(struct in_addr));
1381 pip->ip_len &= 0xff;
1382 (void) send_ip(nfd, mtu, pip, gwip, 0);
1389 for (i = 0; i < 512; i++) {
1390 for (s = (u_char *)pip, j = 0; j < sizeof(tbuf); j++, s++)
1391 *s = (rand() >> 13) & 0xff;
1392 IP_V_A(pip, IPVERSION);
1393 pip->ip_off &= htons(0xc000);
1394 bcopy((char *)&ip->ip_dst, (char *)&pip->ip_dst,
1395 sizeof(struct in_addr));
1397 pip->ip_len &= 0xff;
1398 (void) send_ip(nfd, mtu, pip, gwip, 0);