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.11 2009/01/27 08:33:23 darrenr Exp";
13 #include <sys/param.h>
14 #include <sys/types.h>
16 #if defined(__NetBSD__) && defined(__vax__)
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.
22 typedef int boolean_t
;
27 # include <machine/lock.h>
28 # include <sys/mutex.h>
32 # if !defined(solaris) && !defined(linux) && !defined(__sgi) && !defined(hpux)
33 # include <sys/file.h>
36 # include <sys/dditypes.h>
42 #if !defined(solaris) && !defined(linux) && !defined(__sgi)
44 # include <sys/user.h>
45 # include <sys/proc.h>
47 #if !defined(ultrix) && !defined(hpux) && !defined(linux) && \
48 !defined(__sgi) && !defined(__osf__) && !defined(_AIX51)
52 # include <sys/socket.h>
55 # include <sys/stream.h>
57 # include <sys/socketvar.h>
60 #include <sys/systm.h>
61 #include <sys/session.h>
64 # include <sys/sysctl.h>
65 # include <sys/filedesc.h>
68 #include <netinet/in_systm.h>
69 #include <sys/socket.h>
71 # define _NET_ROUTE_INCLUDED
74 # include "radix_ipf_local.h"
77 #if defined(linux) && (LINUX >= 0200)
78 # include <asm/atomic.h>
81 # if defined(__FreeBSD__)
82 # include "radix_ipf.h"
84 # if !defined(solaris)
85 # include <net/route.h>
88 # define __KERNEL__ /* because there's a macro not wrapped by this */
89 # include <net/route.h> /* in this file :-/ */
91 #include <netinet/in.h>
92 #include <arpa/inet.h>
93 #include <netinet/ip.h>
94 #if defined(__SVR4) || defined(__svr4__) || defined(__sgi)
95 # include <sys/sysmacros.h>
102 # undef _NET_ROUTE_INCLUDED
105 # include <netinet/ip_var.h>
106 # if !defined(__hpux) && !defined(solaris)
107 # include <netinet/in_pcb.h>
111 #if !defined(linux) && !defined(__hpux)
112 # include <netinet/tcp_timer.h>
113 # include <netinet/tcp_var.h>
115 #if defined(__NetBSD_Version__) && (__NetBSD_Version__ >= 106000000)
116 # define USE_NANOSLEEP
121 # define PAUSE() ts.tv_sec = 0; ts.tv_nsec = 10000000; \
122 (void) nanosleep(&ts, NULL)
124 # define PAUSE() tv.tv_sec = 0; tv.tv_usec = 10000; \
125 (void) select(0, NULL, NULL, NULL, &tv)
129 void ip_test1(dev
, mtu
, ip
, gwip
, ptest
)
142 int nfd
, i
= 0, len
, id
= getpid();
144 IP_HL_A(ip
, sizeof(*ip
) >> 2);
145 IP_V_A(ip
, IPVERSION
);
149 ip
->ip_p
= IPPROTO_UDP
;
151 u
= (udphdr_t
*)(ip
+ 1);
152 u
->uh_sport
= htons(1);
153 u
->uh_dport
= htons(9);
155 u
->uh_ulen
= htons(sizeof(*u
) + 4);
156 ip
->ip_len
= sizeof(*ip
) + ntohs(u
->uh_ulen
);
159 nfd
= initdevice(dev
, 1);
163 if (!ptest
|| (ptest
== 1)) {
168 printf("1.1. sending packets with ip_hl < ip_len\n");
169 for (i
= 0; i
< ((sizeof(*ip
) + ntohs(u
->uh_ulen
)) >> 2); i
++) {
171 (void) send_ip(nfd
, 1500, ip
, gwip
, 1);
179 if (!ptest
|| (ptest
== 2)) {
184 printf("1.2. sending packets with ip_hl > ip_len\n");
185 for (; i
< ((sizeof(*ip
) * 2 + ntohs(u
->uh_ulen
)) >> 2); i
++) {
187 (void) send_ip(nfd
, 1500, ip
, gwip
, 1);
195 if (!ptest
|| (ptest
== 3)) {
200 printf("1.3. ip_v < 4\n");
201 IP_HL_A(ip
, sizeof(*ip
) >> 2);
202 for (i
= 0; i
< 4; i
++) {
204 (void) send_ip(nfd
, 1500, ip
, gwip
, 1);
212 if (!ptest
|| (ptest
== 4)) {
217 printf("1.4. ip_v > 4\n");
218 for (i
= 5; i
< 16; i
++) {
220 (void) send_ip(nfd
, 1500, ip
, gwip
, 1);
228 if (!ptest
|| (ptest
== 5)) {
230 * Part5: len < packet
233 IP_V_A(ip
, IPVERSION
);
235 printf("1.5.0 ip_len < packet size (size++, long packets)\n");
236 for (; i
< (ip
->ip_len
* 2); i
++) {
237 ip
->ip_id
= htons(id
++);
239 ip
->ip_sum
= chksum((u_short
*)ip
, IP_HL(ip
) << 2);
240 (void) send_ether(nfd
, (char *)ip
, i
, gwip
);
246 printf("1.5.1 ip_len < packet size (ip_len-, short packets)\n");
247 for (i
= len
; i
> 0; i
--) {
248 ip
->ip_id
= htons(id
++);
251 ip
->ip_sum
= chksum((u_short
*)ip
, IP_HL(ip
) << 2);
252 (void) send_ether(nfd
, (char *)ip
, len
, gwip
);
260 if (!ptest
|| (ptest
== 6)) {
262 * Part6: len > packet
265 printf("1.6.0 ip_len > packet size (increase ip_len)\n");
266 for (i
= len
+ 1; i
< (len
* 2); i
++) {
267 ip
->ip_id
= htons(id
++);
270 ip
->ip_sum
= chksum((u_short
*)ip
, IP_HL(ip
) << 2);
271 (void) send_ether(nfd
, (char *)ip
, len
, gwip
);
278 printf("1.6.1 ip_len > packet size (size--, short packets)\n");
279 for (i
= len
; i
> 0; i
--) {
280 ip
->ip_id
= htons(id
++);
282 ip
->ip_sum
= chksum((u_short
*)ip
, IP_HL(ip
) << 2);
283 (void) send_ether(nfd
, (char *)ip
, i
, gwip
);
291 if (!ptest
|| (ptest
== 7)) {
293 * Part7: 0 length fragment
295 printf("1.7.0 Zero length fragments (ip_off = 0x2000)\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.1 Zero length fragments (ip_off = 0x3000)\n");
305 ip
->ip_len
= sizeof(*ip
);
306 ip
->ip_off
= htons(IP_MF
);
307 (void) send_ip(nfd
, mtu
, ip
, gwip
, 1);
311 printf("1.7.2 Zero length fragments (ip_off = 0xa000)\n");
313 ip
->ip_len
= sizeof(*ip
);
314 ip
->ip_off
= htons(0xa000);
315 (void) send_ip(nfd
, mtu
, ip
, gwip
, 1);
319 printf("1.7.3 Zero length fragments (ip_off = 0x0100)\n");
321 ip
->ip_len
= sizeof(*ip
);
322 ip
->ip_off
= htons(0x0100);
323 (void) send_ip(nfd
, mtu
, ip
, gwip
, 1);
328 if (!ptest
|| (ptest
== 8)) {
331 gettimeofday(&tv
, NULL
);
332 srand(tv
.tv_sec
^ getpid() ^ tv
.tv_usec
);
334 * Part8.1: 63k packet + 1k fragment at offset 0x1ffe
335 * Mark it as being ICMP (so it doesn't get junked), but
336 * don't bother about the ICMP header, we're not worrying
339 ip
->ip_p
= IPPROTO_ICMP
;
340 ip
->ip_off
= htons(IP_MF
);
341 u
->uh_dport
= htons(9);
342 ip
->ip_id
= htons(id
++);
343 printf("1.8.1 63k packet + 1k fragment at offset 0x1ffe\n");
344 ip
->ip_len
= 768 + 20 + 8;
345 (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));
352 (void) send_ip(nfd
, mtu
, ip
, gwip
, 1);
357 ip
->ip_len
= 896 + 20;
358 ip
->ip_off
= htons(i
>> 3);
359 (void) send_ip(nfd
, mtu
, ip
, gwip
, 1);
365 * Part8.2: 63k packet + 1k fragment at offset 0x1ffe
366 * Mark it as being ICMP (so it doesn't get junked), but
367 * don't bother about the ICMP header, we're not worrying
368 * about that here. (Lossage here)
370 ip
->ip_p
= IPPROTO_ICMP
;
371 ip
->ip_off
= htons(IP_MF
);
372 u
->uh_dport
= htons(9);
373 ip
->ip_id
= htons(id
++);
374 printf("1.8.2 63k packet + 1k fragment at offset 0x1ffe\n");
375 ip
->ip_len
= 768 + 20 + 8;
376 if ((rand() & 0x1f) != 0) {
377 (void) send_ip(nfd
, mtu
, ip
, gwip
, 1);
382 ip
->ip_len
= MIN(768 + 20, mtu
- 68);
384 for (; i
< (63 * 1024 + 768); i
+= 768) {
385 ip
->ip_off
= htons(IP_MF
| (i
>> 3));
386 if ((rand() & 0x1f) != 0) {
387 (void) send_ip(nfd
, mtu
, ip
, gwip
, 1);
390 printf("skip %d\n", i
);
394 ip
->ip_len
= 896 + 20;
395 ip
->ip_off
= htons(i
>> 3);
396 if ((rand() & 0x1f) != 0) {
397 (void) send_ip(nfd
, mtu
, ip
, gwip
, 1);
405 * Part8.3: 33k packet - test for not dealing with -ve length
406 * Mark it as being ICMP (so it doesn't get junked), but
407 * don't bother about the ICMP header, we're not worrying
410 ip
->ip_p
= IPPROTO_ICMP
;
411 ip
->ip_off
= htons(IP_MF
);
412 u
->uh_dport
= htons(9);
413 ip
->ip_id
= htons(id
++);
414 printf("1.8.3 33k packet\n");
415 ip
->ip_len
= 768 + 20 + 8;
416 (void) send_ip(nfd
, mtu
, ip
, gwip
, 1);
419 ip
->ip_len
= MIN(768 + 20, mtu
- 68);
421 for (; i
< (32 * 1024 + 768); i
+= 768) {
422 ip
->ip_off
= htons(IP_MF
| (i
>> 3));
423 (void) send_ip(nfd
, mtu
, ip
, gwip
, 1);
428 ip
->ip_len
= 896 + 20;
429 ip
->ip_off
= htons(i
>> 3);
430 (void) send_ip(nfd
, mtu
, ip
, gwip
, 1);
438 if (!ptest
|| (ptest
== 9)) {
440 * Part9: off & 0x8000 == 0x8000
443 ip
->ip_off
= htons(0x8000);
444 printf("1.9. ip_off & 0x8000 == 0x8000\n");
445 (void) send_ip(nfd
, mtu
, ip
, gwip
, 1);
452 if (!ptest
|| (ptest
== 10)) {
458 printf("1.10.0 ip_ttl = 255\n");
459 (void) send_ip(nfd
, mtu
, ip
, gwip
, 1);
464 printf("1.10.1 ip_ttl = 128\n");
465 (void) send_ip(nfd
, mtu
, ip
, gwip
, 1);
470 printf("1.10.2 ip_ttl = 0\n");
471 (void) send_ip(nfd
, mtu
, ip
, gwip
, 1);
480 void ip_test2(dev
, mtu
, ip
, gwip
, ptest
)
496 nfd
= initdevice(dev
, 1);
501 ip
->ip_len
= IP_HL(ip
) << 2;
502 s
= (u_char
*)(ip
+ 1);
503 s
[IPOPT_OPTVAL
] = IPOPT_NOP
;
505 if (!ptest
|| (ptest
== 1)) {
507 * Test 1: option length > packet length,
508 * header length == packet length
510 s
[IPOPT_OPTVAL
] = IPOPT_TS
;
512 s
[IPOPT_OFFSET
] = IPOPT_MINOFF
;
513 ip
->ip_p
= IPPROTO_IP
;
514 printf("2.1 option length > packet length\n");
515 (void) send_ip(nfd
, mtu
, ip
, gwip
, 1);
521 ip
->ip_len
= IP_HL(ip
) << 2;
522 if (!ptest
|| (ptest
== 1)) {
524 * Test 2: options have length = 0
526 printf("2.2.1 option length = 0, RR\n");
527 s
[IPOPT_OPTVAL
] = IPOPT_RR
;
529 (void) send_ip(nfd
, mtu
, ip
, gwip
, 1);
533 printf("2.2.2 option length = 0, TS\n");
534 s
[IPOPT_OPTVAL
] = IPOPT_TS
;
536 (void) send_ip(nfd
, mtu
, ip
, gwip
, 1);
540 printf("2.2.3 option length = 0, SECURITY\n");
541 s
[IPOPT_OPTVAL
] = IPOPT_SECURITY
;
543 (void) send_ip(nfd
, mtu
, ip
, gwip
, 1);
547 printf("2.2.4 option length = 0, LSRR\n");
548 s
[IPOPT_OPTVAL
] = IPOPT_LSRR
;
550 (void) send_ip(nfd
, mtu
, ip
, gwip
, 1);
554 printf("2.2.5 option length = 0, SATID\n");
555 s
[IPOPT_OPTVAL
] = IPOPT_SATID
;
557 (void) send_ip(nfd
, mtu
, ip
, gwip
, 1);
561 printf("2.2.6 option length = 0, SSRR\n");
562 s
[IPOPT_OPTVAL
] = IPOPT_SSRR
;
564 (void) send_ip(nfd
, mtu
, ip
, gwip
, 1);
576 void ip_test3(dev
, mtu
, ip
, gwip
, ptest
)
583 static int ict1
[10] = { 8, 9, 10, 13, 14, 15, 16, 17, 18, 0 };
584 static int ict2
[8] = { 3, 9, 10, 13, 14, 17, 18, 0 };
593 IP_HL_A(ip
, sizeof(*ip
) >> 2);
594 IP_V_A(ip
, IPVERSION
);
598 ip
->ip_p
= IPPROTO_ICMP
;
600 ip
->ip_len
= sizeof(*ip
) + sizeof(*icp
);
601 icp
= (struct icmp
*)((char *)ip
+ (IP_HL(ip
) << 2));
603 nfd
= initdevice(dev
, 1);
607 if (!ptest
|| (ptest
== 1)) {
609 * Type 0 - 31, 255, code = 0
611 bzero((char *)icp
, sizeof(*icp
));
612 for (i
= 0; i
< 32; i
++) {
614 (void) send_icmp(nfd
, mtu
, ip
, gwip
);
616 printf("3.1.%d ICMP type %d code 0 (all 0's)\r", i
, i
);
618 icp
->icmp_type
= 255;
619 (void) send_icmp(nfd
, mtu
, ip
, gwip
);
621 printf("3.1.%d ICMP type %d code 0 (all 0's)\r", i
, 255);
625 if (!ptest
|| (ptest
== 2)) {
627 * Type 3, code = 0 - 31
630 for (i
= 0; i
< 32; i
++) {
632 (void) send_icmp(nfd
, mtu
, ip
, gwip
);
634 printf("3.2.%d ICMP type 3 code %d (all 0's)\r", i
, i
);
638 if (!ptest
|| (ptest
== 3)) {
640 * Type 4, code = 0,127,128,255
644 (void) send_icmp(nfd
, mtu
, ip
, gwip
);
646 printf("3.3.1 ICMP type 4 code 0 (all 0's)\r");
647 icp
->icmp_code
= 127;
648 (void) send_icmp(nfd
, mtu
, ip
, gwip
);
650 printf("3.3.2 ICMP type 4 code 127 (all 0's)\r");
651 icp
->icmp_code
= 128;
652 (void) send_icmp(nfd
, mtu
, ip
, gwip
);
654 printf("3.3.3 ICMP type 4 code 128 (all 0's)\r");
655 icp
->icmp_code
= 255;
656 (void) send_icmp(nfd
, mtu
, ip
, gwip
);
658 printf("3.3.4 ICMP type 4 code 255 (all 0's)\r");
661 if (!ptest
|| (ptest
== 4)) {
663 * Type 5, code = 0,127,128,255
667 (void) send_icmp(nfd
, mtu
, ip
, gwip
);
669 printf("3.4.1 ICMP type 5 code 0 (all 0's)\r");
670 icp
->icmp_code
= 127;
671 (void) send_icmp(nfd
, mtu
, ip
, gwip
);
673 printf("3.4.2 ICMP type 5 code 127 (all 0's)\r");
674 icp
->icmp_code
= 128;
675 (void) send_icmp(nfd
, mtu
, ip
, gwip
);
677 printf("3.4.3 ICMP type 5 code 128 (all 0's)\r");
678 icp
->icmp_code
= 255;
679 (void) send_icmp(nfd
, mtu
, ip
, gwip
);
681 printf("3.4.4 ICMP type 5 code 255 (all 0's)\r");
684 if (!ptest
|| (ptest
== 5)) {
686 * Type 8-10;13-18, code - 0,127,128,255
688 for (i
= 0; ict1
[i
]; i
++) {
689 icp
->icmp_type
= ict1
[i
];
691 (void) send_icmp(nfd
, mtu
, ip
, gwip
);
693 printf("3.5.%d ICMP type 5 code 0 (all 0's)\r",
695 icp
->icmp_code
= 127;
696 (void) send_icmp(nfd
, mtu
, ip
, gwip
);
698 printf("3.5.%d ICMP type 5 code 127 (all 0's)\r",
700 icp
->icmp_code
= 128;
701 (void) send_icmp(nfd
, mtu
, ip
, gwip
);
703 printf("3.5.%d ICMP type 5 code 128 (all 0's)\r",
705 icp
->icmp_code
= 255;
706 (void) send_icmp(nfd
, mtu
, ip
, gwip
);
708 printf("3.5.%d ICMP type 5 code 255 (all 0's)\r",
714 if (!ptest
|| (ptest
== 6)) {
716 * Type 12, code - 0,127,128,129,255
720 (void) send_icmp(nfd
, mtu
, ip
, gwip
);
722 printf("3.6.1 ICMP type 12 code 0 (all 0's)\r");
723 icp
->icmp_code
= 127;
724 (void) send_icmp(nfd
, mtu
, ip
, gwip
);
726 printf("3.6.2 ICMP type 12 code 127 (all 0's)\r");
727 icp
->icmp_code
= 128;
728 (void) send_icmp(nfd
, mtu
, ip
, gwip
);
730 printf("3.6.3 ICMP type 12 code 128 (all 0's)\r");
731 icp
->icmp_code
= 129;
732 (void) send_icmp(nfd
, mtu
, ip
, gwip
);
734 printf("3.6.4 ICMP type 12 code 129 (all 0's)\r");
735 icp
->icmp_code
= 255;
736 (void) send_icmp(nfd
, mtu
, ip
, gwip
);
738 printf("3.6.5 ICMP type 12 code 255 (all 0's)\r");
742 if (!ptest
|| (ptest
== 7)) {
744 * Type 3;9-10;13-14;17-18 - shorter packets
746 ip
->ip_len
= sizeof(*ip
) + sizeof(*icp
) / 2;
747 for (i
= 0; ict2
[i
]; i
++) {
748 icp
->icmp_type
= ict1
[i
];
750 (void) send_icmp(nfd
, mtu
, ip
, gwip
);
752 printf("3.5.%d ICMP type %d code 0 (all 0's)\r",
753 i
* 4, icp
->icmp_type
);
754 icp
->icmp_code
= 127;
755 (void) send_icmp(nfd
, mtu
, ip
, gwip
);
757 printf("3.5.%d ICMP type %d code 127 (all 0's)\r",
758 i
* 4 + 1, icp
->icmp_type
);
759 icp
->icmp_code
= 128;
760 (void) send_icmp(nfd
, mtu
, ip
, gwip
);
762 printf("3.5.%d ICMP type %d code 128 (all 0's)\r",
763 i
* 4 + 2, icp
->icmp_type
);
764 icp
->icmp_code
= 255;
765 (void) send_icmp(nfd
, mtu
, ip
, gwip
);
767 printf("3.5.%d ICMP type %d code 127 (all 0's)\r",
768 i
* 4 + 3, icp
->icmp_type
);
775 /* Perform test 4 (UDP) */
777 void ip_test4(dev
, mtu
, ip
, gwip
, ptest
)
793 IP_HL_A(ip
, sizeof(*ip
) >> 2);
794 IP_V_A(ip
, IPVERSION
);
798 ip
->ip_p
= IPPROTO_UDP
;
800 u
= (udphdr_t
*)((char *)ip
+ (IP_HL(ip
) << 2));
801 u
->uh_sport
= htons(1);
802 u
->uh_dport
= htons(1);
803 u
->uh_ulen
= htons(sizeof(*u
) + 4);
805 nfd
= initdevice(dev
, 1);
809 if (!ptest
|| (ptest
== 1)) {
811 * Test 1. ulen > packet
813 u
->uh_ulen
= htons(sizeof(*u
) + 4);
814 ip
->ip_len
= (IP_HL(ip
) << 2) + ntohs(u
->uh_ulen
);
815 printf("4.1 UDP uh_ulen > packet size - short packets\n");
816 for (i
= ntohs(u
->uh_ulen
) * 2; i
> sizeof(*u
) + 4; i
--) {
817 u
->uh_ulen
= htons(i
);
818 (void) send_udp(nfd
, 1500, ip
, gwip
);
826 if (!ptest
|| (ptest
== 2)) {
828 * Test 2. ulen < packet
830 u
->uh_ulen
= htons(sizeof(*u
) + 4);
831 ip
->ip_len
= (IP_HL(ip
) << 2) + ntohs(u
->uh_ulen
);
832 printf("4.2 UDP uh_ulen < packet size - short packets\n");
833 for (i
= ntohs(u
->uh_ulen
) * 2; i
> sizeof(*u
) + 4; i
--) {
835 (void) send_udp(nfd
, 1500, ip
, gwip
);
843 if (!ptest
|| (ptest
== 3)) {
845 * Test 3: sport = 0, sport = 1, sport = 32767
846 * sport = 32768, sport = 65535
848 u
->uh_ulen
= sizeof(*u
) + 4;
849 ip
->ip_len
= (IP_HL(ip
) << 2) + ntohs(u
->uh_ulen
);
850 printf("4.3.1 UDP sport = 0\n");
852 (void) send_udp(nfd
, 1500, ip
, gwip
);
856 printf("4.3.2 UDP sport = 1\n");
857 u
->uh_sport
= htons(1);
858 (void) send_udp(nfd
, 1500, ip
, gwip
);
862 printf("4.3.3 UDP sport = 32767\n");
863 u
->uh_sport
= htons(32767);
864 (void) send_udp(nfd
, 1500, ip
, gwip
);
868 printf("4.3.4 UDP sport = 32768\n");
869 u
->uh_sport
= htons(32768);
870 (void) send_udp(nfd
, 1500, ip
, gwip
);
875 printf("4.3.5 UDP sport = 65535\n");
876 u
->uh_sport
= htons(65535);
877 (void) send_udp(nfd
, 1500, ip
, gwip
);
883 if (!ptest
|| (ptest
== 4)) {
885 * Test 4: dport = 0, dport = 1, dport = 32767
886 * dport = 32768, dport = 65535
888 u
->uh_ulen
= ntohs(sizeof(*u
) + 4);
889 u
->uh_sport
= htons(1);
890 ip
->ip_len
= (IP_HL(ip
) << 2) + ntohs(u
->uh_ulen
);
891 printf("4.4.1 UDP dport = 0\n");
893 (void) send_udp(nfd
, 1500, ip
, gwip
);
897 printf("4.4.2 UDP dport = 1\n");
898 u
->uh_dport
= htons(1);
899 (void) send_udp(nfd
, 1500, ip
, gwip
);
903 printf("4.4.3 UDP dport = 32767\n");
904 u
->uh_dport
= htons(32767);
905 (void) send_udp(nfd
, 1500, ip
, gwip
);
909 printf("4.4.4 UDP dport = 32768\n");
910 u
->uh_dport
= htons(32768);
911 (void) send_udp(nfd
, 1500, ip
, gwip
);
915 printf("4.4.5 UDP dport = 65535\n");
916 u
->uh_dport
= htons(65535);
917 (void) send_udp(nfd
, 1500, ip
, gwip
);
923 if (!ptest
|| (ptest
== 5)) {
925 * Test 5: sizeof(ip_t) <= MTU <= sizeof(udphdr_t) +
928 printf("4.5 UDP 20 <= MTU <= 32\n");
929 for (i
= sizeof(*ip
); i
<= ntohs(u
->uh_ulen
); i
++) {
930 (void) send_udp(nfd
, i
, ip
, gwip
);
940 /* Perform test 5 (TCP) */
942 void ip_test5(dev
, mtu
, ip
, gwip
, ptest
)
957 t
= (tcphdr_t
*)((char *)ip
+ (IP_HL(ip
) << 2));
958 #if !defined(linux) && !defined(__osf__)
962 t
->th_sport
= htons(1);
963 t
->th_dport
= htons(1);
964 t
->th_win
= htons(4096);
967 t
->th_seq
= htonl(1);
969 ip
->ip_len
= sizeof(ip_t
) + sizeof(tcphdr_t
);
971 nfd
= initdevice(dev
, 1);
975 if (!ptest
|| (ptest
== 1)) {
977 * Test 1: flags variations, 0 - 3f
979 TCP_OFF_A(t
, sizeof(*t
) >> 2);
980 printf("5.1 Test TCP flag combinations\n");
981 for (i
= 0; i
<= (TH_URG
|TH_ACK
|TH_PUSH
|TH_RST
|TH_SYN
|TH_FIN
);
984 (void) send_tcp(nfd
, mtu
, ip
, gwip
);
992 if (!ptest
|| (ptest
== 2)) {
993 t
->th_flags
= TH_SYN
;
995 * Test 2: seq = 0, seq = 1, seq = 0x7fffffff, seq=0x80000000,
996 * seq = 0xa000000, seq = 0xffffffff
998 printf("5.2.1 TCP seq = 0\n");
999 t
->th_seq
= htonl(0);
1000 (void) send_tcp(nfd
, mtu
, ip
, gwip
);
1004 printf("5.2.2 TCP seq = 1\n");
1005 t
->th_seq
= htonl(1);
1006 (void) send_tcp(nfd
, mtu
, ip
, gwip
);
1010 printf("5.2.3 TCP seq = 0x7fffffff\n");
1011 t
->th_seq
= htonl(0x7fffffff);
1012 (void) send_tcp(nfd
, mtu
, ip
, gwip
);
1016 printf("5.2.4 TCP seq = 0x80000000\n");
1017 t
->th_seq
= htonl(0x80000000);
1018 (void) send_tcp(nfd
, mtu
, ip
, gwip
);
1022 printf("5.2.5 TCP seq = 0xc0000000\n");
1023 t
->th_seq
= htonl(0xc0000000);
1024 (void) send_tcp(nfd
, mtu
, ip
, gwip
);
1028 printf("5.2.6 TCP seq = 0xffffffff\n");
1029 t
->th_seq
= htonl(0xffffffff);
1030 (void) send_tcp(nfd
, mtu
, ip
, gwip
);
1035 if (!ptest
|| (ptest
== 3)) {
1036 t
->th_flags
= TH_ACK
;
1038 * Test 3: ack = 0, ack = 1, ack = 0x7fffffff, ack = 0x8000000
1039 * ack = 0xa000000, ack = 0xffffffff
1041 printf("5.3.1 TCP ack = 0\n");
1043 (void) send_tcp(nfd
, mtu
, ip
, gwip
);
1047 printf("5.3.2 TCP ack = 1\n");
1048 t
->th_ack
= htonl(1);
1049 (void) send_tcp(nfd
, mtu
, ip
, gwip
);
1053 printf("5.3.3 TCP ack = 0x7fffffff\n");
1054 t
->th_ack
= htonl(0x7fffffff);
1055 (void) send_tcp(nfd
, mtu
, ip
, gwip
);
1059 printf("5.3.4 TCP ack = 0x80000000\n");
1060 t
->th_ack
= htonl(0x80000000);
1061 (void) send_tcp(nfd
, mtu
, ip
, gwip
);
1065 printf("5.3.5 TCP ack = 0xc0000000\n");
1066 t
->th_ack
= htonl(0xc0000000);
1067 (void) send_tcp(nfd
, mtu
, ip
, gwip
);
1071 printf("5.3.6 TCP ack = 0xffffffff\n");
1072 t
->th_ack
= htonl(0xffffffff);
1073 (void) send_tcp(nfd
, mtu
, ip
, gwip
);
1078 if (!ptest
|| (ptest
== 4)) {
1079 t
->th_flags
= TH_SYN
;
1081 * Test 4: win = 0, win = 32768, win = 65535
1083 printf("5.4.1 TCP win = 0\n");
1084 t
->th_seq
= htonl(0);
1085 (void) send_tcp(nfd
, mtu
, ip
, gwip
);
1089 printf("5.4.2 TCP win = 32768\n");
1090 t
->th_seq
= htonl(0x7fff);
1091 (void) send_tcp(nfd
, mtu
, ip
, gwip
);
1095 printf("5.4.3 TCP win = 65535\n");
1096 t
->th_win
= htons(0xffff);
1097 (void) send_tcp(nfd
, mtu
, ip
, gwip
);
1102 #if !defined(linux) && !defined(__SVR4) && !defined(__svr4__) && \
1103 !defined(__sgi) && !defined(__hpux) && !defined(__osf__)
1105 struct tcpcb
*tcbp
, tcb
;
1107 struct sockaddr_in sin
;
1111 bzero((char *)&sin
, sizeof(sin
));
1113 for (i
= 1; i
< 63; i
++) {
1114 fd
= socket(AF_INET
, SOCK_STREAM
, 0);
1115 bzero((char *)&sin
, sizeof(sin
));
1116 sin
.sin_addr
.s_addr
= ip
->ip_dst
.s_addr
;
1117 sin
.sin_port
= htons(i
);
1118 sin
.sin_family
= AF_INET
;
1119 if (!connect(fd
, (struct sockaddr
*)&sin
, sizeof(sin
)))
1125 printf("Couldn't open a TCP socket between ports 1 and 63\n");
1126 printf("to host %s for test 5 and 6 - skipping.\n",
1127 inet_ntoa(ip
->ip_dst
));
1128 goto skip_five_and_six
;
1131 bcopy((char *)ip
, (char *)&ti
, sizeof(*ip
));
1132 t
->th_dport
= htons(i
);
1134 if (!getsockname(fd
, (struct sockaddr
*)&sin
, &slen
))
1135 t
->th_sport
= sin
.sin_port
;
1136 if (!(tcbp
= find_tcp(fd
, &ti
))) {
1137 printf("Can't find PCB\n");
1138 goto skip_five_and_six
;
1140 KMCPY(&tcb
, tcbp
, sizeof(tcb
));
1141 ti
.ti_win
= tcb
.rcv_adv
;
1142 ti
.ti_seq
= htonl(tcb
.snd_nxt
- 1);
1143 ti
.ti_ack
= tcb
.rcv_nxt
;
1145 if (!ptest
|| (ptest
== 5)) {
1149 t
->th_flags
= TH_ACK
|TH_URG
;
1150 printf("5.5.1 TCP Urgent pointer, sport %hu dport %hu\n",
1151 ntohs(t
->th_sport
), ntohs(t
->th_dport
));
1152 t
->th_urp
= htons(1);
1153 (void) send_tcp(nfd
, mtu
, ip
, gwip
);
1156 t
->th_seq
= htonl(tcb
.snd_nxt
);
1157 ip
->ip_len
= sizeof(ip_t
) + sizeof(tcphdr_t
) + 1;
1158 t
->th_urp
= htons(0x7fff);
1159 (void) send_tcp(nfd
, mtu
, ip
, gwip
);
1161 t
->th_urp
= htons(0x8000);
1162 (void) send_tcp(nfd
, mtu
, ip
, gwip
);
1164 t
->th_urp
= htons(0xffff);
1165 (void) send_tcp(nfd
, mtu
, ip
, gwip
);
1168 t
->th_flags
&= ~TH_URG
;
1169 ip
->ip_len
= sizeof(ip_t
) + sizeof(tcphdr_t
);
1172 if (!ptest
|| (ptest
== 6)) {
1174 * Test 6: data offset, off = 0, off is inside, off is outside
1176 t
->th_flags
= TH_ACK
;
1177 printf("5.6.1 TCP off = 1-15, len = 40\n");
1178 for (i
= 1; i
< 16; i
++) {
1179 TCP_OFF_A(t
, ntohs(i
));
1180 (void) send_tcp(nfd
, mtu
, ip
, gwip
);
1186 ip
->ip_len
= sizeof(ip_t
) + sizeof(tcphdr_t
);
1193 t
->th_seq
= htonl(1);
1194 t
->th_ack
= htonl(1);
1197 if (!ptest
|| (ptest
== 7)) {
1198 t
->th_flags
= TH_SYN
;
1200 * Test 7: sport = 0, sport = 1, sport = 32767
1201 * sport = 32768, sport = 65535
1203 printf("5.7.1 TCP sport = 0\n");
1205 (void) send_tcp(nfd
, mtu
, ip
, gwip
);
1209 printf("5.7.2 TCP sport = 1\n");
1210 t
->th_sport
= htons(1);
1211 (void) send_tcp(nfd
, mtu
, ip
, gwip
);
1215 printf("5.7.3 TCP sport = 32767\n");
1216 t
->th_sport
= htons(32767);
1217 (void) send_tcp(nfd
, mtu
, ip
, gwip
);
1221 printf("5.7.4 TCP sport = 32768\n");
1222 t
->th_sport
= htons(32768);
1223 (void) send_tcp(nfd
, mtu
, ip
, gwip
);
1227 printf("5.7.5 TCP sport = 65535\n");
1228 t
->th_sport
= htons(65535);
1229 (void) send_tcp(nfd
, mtu
, ip
, gwip
);
1234 if (!ptest
|| (ptest
== 8)) {
1235 t
->th_sport
= htons(1);
1236 t
->th_flags
= TH_SYN
;
1238 * Test 8: dport = 0, dport = 1, dport = 32767
1239 * dport = 32768, dport = 65535
1241 printf("5.8.1 TCP dport = 0\n");
1243 (void) send_tcp(nfd
, mtu
, ip
, gwip
);
1247 printf("5.8.2 TCP dport = 1\n");
1248 t
->th_dport
= htons(1);
1249 (void) send_tcp(nfd
, mtu
, ip
, gwip
);
1253 printf("5.8.3 TCP dport = 32767\n");
1254 t
->th_dport
= htons(32767);
1255 (void) send_tcp(nfd
, mtu
, ip
, gwip
);
1259 printf("5.8.4 TCP dport = 32768\n");
1260 t
->th_dport
= htons(32768);
1261 (void) send_tcp(nfd
, mtu
, ip
, gwip
);
1265 printf("5.8.5 TCP dport = 65535\n");
1266 t
->th_dport
= htons(65535);
1267 (void) send_tcp(nfd
, mtu
, ip
, gwip
);
1272 /* LAND attack - self connect, so make src & dst ip/port the same */
1273 if (!ptest
|| (ptest
== 9)) {
1274 printf("5.9 TCP LAND attack. sport = 25, dport = 25\n");
1275 /* chose SMTP port 25 */
1276 t
->th_sport
= htons(25);
1277 t
->th_dport
= htons(25);
1278 t
->th_flags
= TH_SYN
;
1279 ip
->ip_src
= ip
->ip_dst
;
1280 (void) send_tcp(nfd
, mtu
, ip
, gwip
);
1285 /* TCP options header checking */
1286 /* 0 length options, etc */
1290 /* Perform test 6 (exhaust mbuf test) */
1292 void ip_test6(dev
, mtu
, ip
, gwip
, ptest
)
1296 struct in_addr gwip
;
1299 #ifdef USE_NANOSLEEP
1307 IP_V_A(ip
, IPVERSION
);
1311 ip
->ip_p
= IPPROTO_UDP
;
1313 u
= (udphdr_t
*)(ip
+ 1);
1314 u
->uh_sport
= htons(1);
1315 u
->uh_dport
= htons(9);
1318 nfd
= initdevice(dev
, 1);
1322 u
->uh_ulen
= htons(7168);
1324 printf("6. Exhaustive mbuf test.\n");
1325 printf(" Send 7k packet in 768 & 128 byte fragments, 128 times.\n");
1326 printf(" Total of around 8,900 packets\n");
1327 for (i
= 0; i
< 128; i
++) {
1329 * First send the entire packet in 768 byte chunks.
1331 ip
->ip_len
= sizeof(*ip
) + 768 + sizeof(*u
);
1332 IP_HL_A(ip
, sizeof(*ip
) >> 2);
1333 ip
->ip_off
= htons(IP_MF
);
1334 (void) send_ip(nfd
, 1500, ip
, gwip
, 1);
1335 printf("%d %d\r", i
, 0);
1339 * And again using 128 byte chunks.
1341 ip
->ip_len
= sizeof(*ip
) + 128 + sizeof(*u
);
1342 ip
->ip_off
= htons(IP_MF
);
1343 (void) send_ip(nfd
, 1500, ip
, gwip
, 1);
1344 printf("%d %d\r", i
, 0);
1348 for (j
= 768; j
< 3584; j
+= 768) {
1349 ip
->ip_len
= sizeof(*ip
) + 768;
1350 ip
->ip_off
= htons(IP_MF
|(j
>>3));
1351 (void) send_ip(nfd
, 1500, ip
, gwip
, 1);
1352 printf("%d %d\r", i
, j
);
1356 ip
->ip_len
= sizeof(*ip
) + 128;
1357 for (k
= j
- 768; k
< j
; k
+= 128) {
1358 ip
->ip_off
= htons(IP_MF
|(k
>>3));
1359 (void) send_ip(nfd
, 1500, ip
, gwip
, 1);
1360 printf("%d %d\r", i
, k
);
1370 /* Perform test 7 (random packets) */
1372 static u_long tbuf
[64];
1374 void ip_test7(dev
, mtu
, ip
, gwip
, ptest
)
1378 struct in_addr gwip
;
1382 #ifdef USE_NANOSLEEP
1390 nfd
= initdevice(dev
, 1);
1396 srand(time(NULL
) ^ (getpid() * getppid()));
1398 printf("7. send 1024 random IP packets.\n");
1400 for (i
= 0; i
< 512; i
++) {
1401 for (s
= (u_char
*)pip
, j
= 0; j
< sizeof(tbuf
); j
++, s
++)
1402 *s
= (rand() >> 13) & 0xff;
1403 IP_V_A(pip
, IPVERSION
);
1404 bcopy((char *)&ip
->ip_dst
, (char *)&pip
->ip_dst
,
1405 sizeof(struct in_addr
));
1407 pip
->ip_len
&= 0xff;
1408 (void) send_ip(nfd
, mtu
, pip
, gwip
, 0);
1415 for (i
= 0; i
< 512; i
++) {
1416 for (s
= (u_char
*)pip
, j
= 0; j
< sizeof(tbuf
); j
++, s
++)
1417 *s
= (rand() >> 13) & 0xff;
1418 IP_V_A(pip
, IPVERSION
);
1419 pip
->ip_off
&= htons(0xc000);
1420 bcopy((char *)&ip
->ip_dst
, (char *)&pip
->ip_dst
,
1421 sizeof(struct in_addr
));
1423 pip
->ip_len
&= 0xff;
1424 (void) send_ip(nfd
, mtu
, pip
, gwip
, 0);