]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/ipfilter/ipsend/iptests.c
This commit was generated by cvs2svn to compensate for changes in r171322,
[FreeBSD/FreeBSD.git] / contrib / ipfilter / ipsend / iptests.c
1 /*      $FreeBSD$       */
2
3 /*
4  * Copyright (C) 1993-1998 by Darren Reed.
5  *
6  * See the IPFILTER.LICENCE file for details on licencing.
7  *
8  */
9 #if !defined(lint)
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 $";
12 #endif
13 #include <sys/param.h>
14 #include <sys/types.h>
15 #if defined(__NetBSD__) && defined(__vax__)
16 /*
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.
20  */
21 typedef int     boolean_t;
22 #endif
23 #include <sys/time.h>
24 #if !defined(__osf__)
25 # define _KERNEL
26 # define KERNEL
27 # if !defined(solaris) && !defined(linux) && !defined(__sgi) && !defined(hpux)
28 #  include <sys/file.h>
29 # else
30 #  ifdef solaris
31 #   include <sys/dditypes.h>
32 #  endif
33 # endif
34 # undef  _KERNEL
35 # undef  KERNEL
36 #endif
37 #if !defined(solaris) && !defined(linux) && !defined(__sgi)
38 # include <nlist.h>
39 # include <sys/user.h>
40 # include <sys/proc.h>
41 #endif
42 #if !defined(ultrix) && !defined(hpux) && !defined(linux) && \
43     !defined(__sgi) && !defined(__osf__) && !defined(_AIX51)
44 # include <kvm.h>
45 #endif
46 #ifndef ultrix
47 # include <sys/socket.h>
48 #endif
49 #if defined(solaris)
50 # include <sys/stream.h>
51 #endif
52 #include <sys/socketvar.h>
53 #ifdef sun
54 #include <sys/systm.h>
55 #include <sys/session.h>
56 #endif
57 #if BSD >= 199103
58 # include <sys/sysctl.h>
59 # include <sys/filedesc.h>
60 # include <paths.h>
61 #endif
62 #include <netinet/in_systm.h>
63 #include <sys/socket.h>
64 #ifdef __hpux
65 # define _NET_ROUTE_INCLUDED
66 #endif
67 #ifdef __osf__
68 # include "radix_ipf_local.h"
69 #endif
70 #include <net/if.h>
71 #if defined(linux) && (LINUX >= 0200)
72 # include <asm/atomic.h>
73 #endif
74 #if !defined(linux)
75 # if defined(__FreeBSD__)
76 #  include "radix_ipf.h"
77 # endif
78 # include <net/route.h>
79 #else
80 # define __KERNEL__     /* because there's a macro not wrapped by this */
81 # include <net/route.h> /* in this file :-/ */
82 #endif
83 #include <netinet/in.h>
84 #include <arpa/inet.h>
85 #include <netinet/ip.h>
86 #if !defined(linux)
87 # include <netinet/ip_var.h>
88 # if !defined(__hpux)
89 #  include <netinet/in_pcb.h>
90 # endif
91 #endif
92 #if defined(__SVR4) || defined(__svr4__) || defined(__sgi)
93 # include <sys/sysmacros.h>
94 #endif
95 #include <stdio.h>
96 #include <unistd.h>
97 #include <stdlib.h>
98 #include <string.h>
99 #ifdef __hpux
100 # undef _NET_ROUTE_INCLUDED
101 #endif
102 #include "ipsend.h"
103 #if !defined(linux) && !defined(__hpux)
104 # include <netinet/tcp_timer.h>
105 # include <netinet/tcp_var.h>
106 #endif
107 #if defined(__NetBSD_Version__) && (__NetBSD_Version__ >= 106000000)
108 # define USE_NANOSLEEP
109 #endif
110
111
112 #ifdef USE_NANOSLEEP
113 # define        PAUSE() ts.tv_sec = 0; ts.tv_nsec = 10000000; \
114                   (void) nanosleep(&ts, NULL)
115 #else
116 # define        PAUSE() tv.tv_sec = 0; tv.tv_usec = 10000; \
117                   (void) select(0, NULL, NULL, NULL, &tv)
118 #endif
119
120
121 void    ip_test1(dev, mtu, ip, gwip, ptest)
122 char    *dev;
123 int     mtu;
124 ip_t    *ip;
125 struct  in_addr gwip;
126 int     ptest;
127 {
128 #ifdef USE_NANOSLEEP
129         struct  timespec ts;
130 #else
131         struct  timeval tv;
132 #endif
133         udphdr_t *u;
134         int     nfd, i = 0, len, id = getpid();
135
136         IP_HL_A(ip, sizeof(*ip) >> 2);
137         IP_V_A(ip, IPVERSION);
138         ip->ip_tos = 0;
139         ip->ip_off = 0;
140         ip->ip_ttl = 60;
141         ip->ip_p = IPPROTO_UDP;
142         ip->ip_sum = 0;
143         u = (udphdr_t *)(ip + 1);
144         u->uh_sport = htons(1);
145         u->uh_dport = htons(9);
146         u->uh_sum = 0;
147         u->uh_ulen = htons(sizeof(*u) + 4);
148         ip->ip_len = sizeof(*ip) + ntohs(u->uh_ulen);
149         len = ip->ip_len;
150
151         nfd = initdevice(dev, 1);
152         if (nfd == -1)
153                 return;
154
155         if (!ptest || (ptest == 1)) {
156                 /*
157                  * Part1: hl < len
158                  */
159                 ip->ip_id = 0;
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++) {
162                         IP_HL_A(ip, i >> 2);
163                         (void) send_ip(nfd, 1500, ip, gwip, 1);
164                         printf("%d\r", i);
165                         fflush(stdout);
166                         PAUSE();
167                 }
168                 putchar('\n');
169         }
170
171         if (!ptest || (ptest == 2)) {
172                 /*
173                  * Part2: hl > len
174                  */
175                 ip->ip_id = 0;
176                 printf("1.2. sending packets with ip_hl > ip_len\n");
177                 for (; i < ((sizeof(*ip) * 2 + ntohs(u->uh_ulen)) >> 2); i++) {
178                         IP_HL_A(ip, i >> 2);
179                         (void) send_ip(nfd, 1500, ip, gwip, 1);
180                         printf("%d\r", i);
181                         fflush(stdout);
182                         PAUSE();
183                 }
184                 putchar('\n');
185         }
186
187         if (!ptest || (ptest == 3)) {
188                 /*
189                  * Part3: v < 4
190                  */
191                 ip->ip_id = 0;
192                 printf("1.3. ip_v < 4\n");
193                 IP_HL_A(ip, sizeof(*ip) >> 2);
194                 for (i = 0; i < 4; i++) {
195                         IP_V_A(ip, i);
196                         (void) send_ip(nfd, 1500, ip, gwip, 1);
197                         printf("%d\r", i);
198                         fflush(stdout);
199                         PAUSE();
200                 }
201                 putchar('\n');
202         }
203
204         if (!ptest || (ptest == 4)) {
205                 /*
206                  * Part4: v > 4
207                  */
208                 ip->ip_id = 0;
209                 printf("1.4. ip_v > 4\n");
210                 for (i = 5; i < 16; i++) {
211                         IP_V_A(ip, i);
212                         (void) send_ip(nfd, 1500, ip, gwip, 1);
213                         printf("%d\r", i);
214                         fflush(stdout);
215                         PAUSE();
216                 }
217                 putchar('\n');
218         }
219
220         if (!ptest || (ptest == 5)) {
221                 /*
222                  * Part5: len < packet
223                  */
224                 ip->ip_id = 0;
225                 IP_V_A(ip, IPVERSION);
226                 i = ip->ip_len + 1;
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++);
230                         ip->ip_sum = 0;
231                         ip->ip_sum = chksum((u_short *)ip, IP_HL(ip) << 2);
232                         (void) send_ether(nfd, (char *)ip, i, gwip);
233                         printf("%d\r", i);
234                         fflush(stdout);
235                         PAUSE();
236                 }
237                 putchar('\n');
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++);
241                         ip->ip_len = i;
242                         ip->ip_sum = 0;
243                         ip->ip_sum = chksum((u_short *)ip, IP_HL(ip) << 2);
244                         (void) send_ether(nfd, (char *)ip, len, gwip);
245                         printf("%d\r", i);
246                         fflush(stdout);
247                         PAUSE();
248                 }
249                 putchar('\n');
250         }
251
252         if (!ptest || (ptest == 6)) {
253                 /*
254                  * Part6: len > packet
255                  */
256                 ip->ip_id = 0;
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++);
260                         ip->ip_len = i;
261                         ip->ip_sum = 0;
262                         ip->ip_sum = chksum((u_short *)ip, IP_HL(ip) << 2);
263                         (void) send_ether(nfd, (char *)ip, len, gwip);
264                         printf("%d\r", i);
265                         fflush(stdout);
266                         PAUSE();
267                 }
268                 putchar('\n');
269                 ip->ip_len = len;
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++);
273                         ip->ip_sum = 0;
274                         ip->ip_sum = chksum((u_short *)ip, IP_HL(ip) << 2);
275                         (void) send_ether(nfd, (char *)ip, i, gwip);
276                         printf("%d\r", i);
277                         fflush(stdout);
278                         PAUSE();
279                 }
280                 putchar('\n');
281         }
282
283         if (!ptest || (ptest == 7)) {
284                 /*
285                  * Part7: 0 length fragment
286                  */
287                 printf("1.7.0 Zero length fragments (ip_off = 0x2000)\n");
288                 ip->ip_id = 0;
289                 ip->ip_len = sizeof(*ip);
290                 ip->ip_off = htons(IP_MF);
291                 (void) send_ip(nfd, mtu, ip, gwip, 1);
292                 fflush(stdout);
293                 PAUSE();
294
295                 printf("1.7.1 Zero length fragments (ip_off = 0x3000)\n");
296                 ip->ip_id = 0;
297                 ip->ip_len = sizeof(*ip);
298                 ip->ip_off = htons(IP_MF);
299                 (void) send_ip(nfd, mtu, ip, gwip, 1);
300                 fflush(stdout);
301                 PAUSE();
302
303                 printf("1.7.2 Zero length fragments (ip_off = 0xa000)\n");
304                 ip->ip_id = 0;
305                 ip->ip_len = sizeof(*ip);
306                 ip->ip_off = htons(0xa000);
307                 (void) send_ip(nfd, mtu, ip, gwip, 1);
308                 fflush(stdout);
309                 PAUSE();
310
311                 printf("1.7.3 Zero length fragments (ip_off = 0x0100)\n");
312                 ip->ip_id = 0;
313                 ip->ip_len = sizeof(*ip);
314                 ip->ip_off = htons(0x0100);
315                 (void) send_ip(nfd, mtu, ip, gwip, 1);
316                 fflush(stdout);
317                 PAUSE();
318         }
319
320         if (!ptest || (ptest == 8)) {
321                 struct  timeval tv;
322
323                 gettimeofday(&tv, NULL);
324                 srand(tv.tv_sec ^ getpid() ^ tv.tv_usec);
325                 /*
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
329                  * about that here.
330                  */
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);
338                 printf("%d\r", i);
339
340                 ip->ip_len = MIN(768 + 20, mtu - 68);
341                 i = 512;
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);
345                         printf("%d\r", i);
346                         fflush(stdout);
347                         PAUSE();
348                 }
349                 ip->ip_len = 896 + 20;
350                 ip->ip_off = htons(i >> 3);
351                 (void) send_ip(nfd, mtu, ip, gwip, 1);
352                 printf("%d\r", i);
353                 putchar('\n');
354                 fflush(stdout);
355
356                 /*
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)
361                  */
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);
370                         printf("%d\r", i);
371                 } else
372                         printf("skip 0\n");
373
374                 ip->ip_len = MIN(768 + 20, mtu - 68);
375                 i = 512;
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);
380                                 printf("%d\r", i);
381                         } else
382                                 printf("skip %d\n", i);
383                         fflush(stdout);
384                         PAUSE();
385                 }
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);
390                         printf("%d\r", i);
391                 } else
392                         printf("skip\n");
393                 putchar('\n');
394                 fflush(stdout);
395
396                 /*
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
400                  * about that here.
401                  */
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);
409                 printf("%d\r", i);
410
411                 ip->ip_len = MIN(768 + 20, mtu - 68);
412                 i = 512;
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);
416                         printf("%d\r", i);
417                         fflush(stdout);
418                         PAUSE();
419                 }
420                 ip->ip_len = 896 + 20;
421                 ip->ip_off = htons(i >> 3);
422                 (void) send_ip(nfd, mtu, ip, gwip, 1);
423                 printf("%d\r", i);
424                 putchar('\n');
425                 fflush(stdout);
426         }
427
428         ip->ip_len = len;
429         ip->ip_off = 0;
430         if (!ptest || (ptest == 9)) {
431                 /*
432                  * Part9: off & 0x8000 == 0x8000
433                  */
434                 ip->ip_id = 0;
435                 ip->ip_off = htons(0x8000);
436                 printf("1.9. ip_off & 0x8000 == 0x8000\n");
437                 (void) send_ip(nfd, mtu, ip, gwip, 1);
438                 fflush(stdout);
439                 PAUSE();
440         }
441
442         ip->ip_off = 0;
443
444         if (!ptest || (ptest == 10)) {
445                 /*
446                  * Part10: ttl = 255
447                  */
448                 ip->ip_id = 0;
449                 ip->ip_ttl = 255;
450                 printf("1.10.0 ip_ttl = 255\n");
451                 (void) send_ip(nfd, mtu, ip, gwip, 1);
452                 fflush(stdout);
453                 PAUSE();
454
455                 ip->ip_ttl = 128;
456                 printf("1.10.1 ip_ttl = 128\n");
457                 (void) send_ip(nfd, mtu, ip, gwip, 1);
458                 fflush(stdout);
459                 PAUSE();
460
461                 ip->ip_ttl = 0;
462                 printf("1.10.2 ip_ttl = 0\n");
463                 (void) send_ip(nfd, mtu, ip, gwip, 1);
464                 fflush(stdout);
465                 PAUSE();
466         }
467
468         (void) close(nfd);
469 }
470
471
472 void    ip_test2(dev, mtu, ip, gwip, ptest)
473 char    *dev;
474 int     mtu;
475 ip_t    *ip;
476 struct  in_addr gwip;
477 int     ptest;
478 {
479 #ifdef USE_NANOSLEEP
480         struct  timespec ts;
481 #else
482         struct  timeval tv;
483 #endif
484         int     nfd;
485         u_char  *s;
486
487
488         nfd = initdevice(dev, 1);
489         if (nfd == -1)
490                 return;
491
492         IP_HL_A(ip, 6);
493         ip->ip_len = IP_HL(ip) << 2;
494         s = (u_char *)(ip + 1);
495         s[IPOPT_OPTVAL] = IPOPT_NOP;
496         s++;
497         if (!ptest || (ptest == 1)) {
498                 /*
499                  * Test 1: option length > packet length,
500                  *                header length == packet length
501                  */
502                 s[IPOPT_OPTVAL] = IPOPT_TS;
503                 s[IPOPT_OLEN] = 4;
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);
508                 fflush(stdout);
509                 PAUSE();
510         }
511
512         IP_HL_A(ip, 7);
513         ip->ip_len = IP_HL(ip) << 2;
514         if (!ptest || (ptest == 1)) {
515                 /*
516                  * Test 2: options have length = 0
517                  */
518                 printf("2.2.1 option length = 0, RR\n");
519                 s[IPOPT_OPTVAL] = IPOPT_RR;
520                 s[IPOPT_OLEN] = 0;
521                 (void) send_ip(nfd, mtu, ip, gwip, 1);
522                 fflush(stdout);
523                 PAUSE();
524
525                 printf("2.2.2 option length = 0, TS\n");
526                 s[IPOPT_OPTVAL] = IPOPT_TS;
527                 s[IPOPT_OLEN] = 0;
528                 (void) send_ip(nfd, mtu, ip, gwip, 1);
529                 fflush(stdout);
530                 PAUSE();
531
532                 printf("2.2.3 option length = 0, SECURITY\n");
533                 s[IPOPT_OPTVAL] = IPOPT_SECURITY;
534                 s[IPOPT_OLEN] = 0;
535                 (void) send_ip(nfd, mtu, ip, gwip, 1);
536                 fflush(stdout);
537                 PAUSE();
538
539                 printf("2.2.4 option length = 0, LSRR\n");
540                 s[IPOPT_OPTVAL] = IPOPT_LSRR;
541                 s[IPOPT_OLEN] = 0;
542                 (void) send_ip(nfd, mtu, ip, gwip, 1);
543                 fflush(stdout);
544                 PAUSE();
545
546                 printf("2.2.5 option length = 0, SATID\n");
547                 s[IPOPT_OPTVAL] = IPOPT_SATID;
548                 s[IPOPT_OLEN] = 0;
549                 (void) send_ip(nfd, mtu, ip, gwip, 1);
550                 fflush(stdout);
551                 PAUSE();
552
553                 printf("2.2.6 option length = 0, SSRR\n");
554                 s[IPOPT_OPTVAL] = IPOPT_SSRR;
555                 s[IPOPT_OLEN] = 0;
556                 (void) send_ip(nfd, mtu, ip, gwip, 1);
557                 fflush(stdout);
558                 PAUSE();
559         }
560
561         (void) close(nfd);
562 }
563
564
565 /*
566  * test 3 (ICMP)
567  */
568 void    ip_test3(dev, mtu, ip, gwip, ptest)
569 char    *dev;
570 int     mtu;
571 ip_t    *ip;
572 struct  in_addr gwip;
573 int     ptest;
574 {
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 };
577 #ifdef USE_NANOSLEEP
578         struct  timespec ts;
579 #else
580         struct  timeval tv;
581 #endif
582         struct  icmp    *icp;
583         int     nfd, i;
584
585         IP_HL_A(ip, sizeof(*ip) >> 2);
586         IP_V_A(ip, IPVERSION);
587         ip->ip_tos = 0;
588         ip->ip_off = 0;
589         ip->ip_ttl = 60;
590         ip->ip_p = IPPROTO_ICMP;
591         ip->ip_sum = 0;
592         ip->ip_len = sizeof(*ip) + sizeof(*icp);
593         icp = (struct icmp *)((char *)ip + (IP_HL(ip) << 2));
594
595         nfd = initdevice(dev, 1);
596         if (nfd == -1)
597                 return;
598
599         if (!ptest || (ptest == 1)) {
600                 /*
601                  * Type 0 - 31, 255, code = 0
602                  */
603                 bzero((char *)icp, sizeof(*icp));
604                 for (i = 0; i < 32; i++) {
605                         icp->icmp_type = i;
606                         (void) send_icmp(nfd, mtu, ip, gwip);
607                         PAUSE();
608                         printf("3.1.%d ICMP type %d code 0 (all 0's)\r", i, i);
609                 }
610                 icp->icmp_type = 255;
611                 (void) send_icmp(nfd, mtu, ip, gwip);
612                 PAUSE();
613                 printf("3.1.%d ICMP type %d code 0 (all 0's)\r", i, 255);
614                 putchar('\n');
615         }
616
617         if (!ptest || (ptest == 2)) {
618                 /*
619                  * Type 3, code = 0 - 31
620                  */
621                 icp->icmp_type = 3;
622                 for (i = 0; i < 32; i++) {
623                         icp->icmp_code = i;
624                         (void) send_icmp(nfd, mtu, ip, gwip);
625                         PAUSE();
626                         printf("3.2.%d ICMP type 3 code %d (all 0's)\r", i, i);
627                 }
628         }
629
630         if (!ptest || (ptest == 3)) {
631                 /*
632                  * Type 4, code = 0,127,128,255
633                  */
634                 icp->icmp_type = 4;
635                 icp->icmp_code = 0;
636                 (void) send_icmp(nfd, mtu, ip, gwip);
637                 PAUSE();
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);
641                 PAUSE();
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);
645                 PAUSE();
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);
649                 PAUSE();
650                 printf("3.3.4 ICMP type 4 code 255 (all 0's)\r");
651         }
652
653         if (!ptest || (ptest == 4)) {
654                 /*
655                  * Type 5, code = 0,127,128,255
656                  */
657                 icp->icmp_type = 5;
658                 icp->icmp_code = 0;
659                 (void) send_icmp(nfd, mtu, ip, gwip);
660                 PAUSE();
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);
664                 PAUSE();
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);
668                 PAUSE();
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);
672                 PAUSE();
673                 printf("3.4.4 ICMP type 5 code 255 (all 0's)\r");
674         }
675
676         if (!ptest || (ptest == 5)) {
677                 /*
678                  * Type 8-10;13-18, code - 0,127,128,255
679                  */
680                 for (i = 0; ict1[i]; i++) {
681                         icp->icmp_type = ict1[i];
682                         icp->icmp_code = 0;
683                         (void) send_icmp(nfd, mtu, ip, gwip);
684                         PAUSE();
685                         printf("3.5.%d ICMP type 5 code 0 (all 0's)\r",
686                                 i * 4);
687                         icp->icmp_code = 127;
688                         (void) send_icmp(nfd, mtu, ip, gwip);
689                         PAUSE();
690                         printf("3.5.%d ICMP type 5 code 127 (all 0's)\r",
691                                 i * 4 + 1);
692                         icp->icmp_code = 128;
693                         (void) send_icmp(nfd, mtu, ip, gwip);
694                         PAUSE();
695                         printf("3.5.%d ICMP type 5 code 128 (all 0's)\r",
696                                 i * 4 + 2);
697                         icp->icmp_code = 255;
698                         (void) send_icmp(nfd, mtu, ip, gwip);
699                         PAUSE();
700                         printf("3.5.%d ICMP type 5 code 255 (all 0's)\r",
701                                 i * 4 + 3);
702                 }
703                 putchar('\n');
704         }
705
706         if (!ptest || (ptest == 6)) {
707                 /*
708                  * Type 12, code - 0,127,128,129,255
709                  */
710                 icp->icmp_type = 12;
711                 icp->icmp_code = 0;
712                 (void) send_icmp(nfd, mtu, ip, gwip);
713                 PAUSE();
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);
717                 PAUSE();
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);
721                 PAUSE();
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);
725                 PAUSE();
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);
729                 PAUSE();
730                 printf("3.6.5 ICMP type 12 code 255 (all 0's)\r");
731                 putchar('\n');
732         }
733
734         if (!ptest || (ptest == 7)) {
735                 /*
736                  * Type 3;9-10;13-14;17-18 - shorter packets
737                  */
738                 ip->ip_len = sizeof(*ip) + sizeof(*icp) / 2;
739                 for (i = 0; ict2[i]; i++) {
740                         icp->icmp_type = ict1[i];
741                         icp->icmp_code = 0;
742                         (void) send_icmp(nfd, mtu, ip, gwip);
743                         PAUSE();
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);
748                         PAUSE();
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);
753                         PAUSE();
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);
758                         PAUSE();
759                         printf("3.5.%d ICMP type %d code 127 (all 0's)\r",
760                                 i * 4 + 3, icp->icmp_type);
761                 }
762                 putchar('\n');
763         }
764 }
765
766
767 /* Perform test 4 (UDP) */
768
769 void    ip_test4(dev, mtu, ip, gwip, ptest)
770 char    *dev;
771 int     mtu;
772 ip_t    *ip;
773 struct  in_addr gwip;
774 int     ptest;
775 {
776 #ifdef USE_NANOSLEEP
777         struct  timespec ts;
778 #else
779         struct  timeval tv;
780 #endif
781         udphdr_t        *u;
782         int     nfd, i;
783
784
785         IP_HL_A(ip, sizeof(*ip) >> 2);
786         IP_V_A(ip, IPVERSION);
787         ip->ip_tos = 0;
788         ip->ip_off = 0;
789         ip->ip_ttl = 60;
790         ip->ip_p = IPPROTO_UDP;
791         ip->ip_sum = 0;
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);
796
797         nfd = initdevice(dev, 1);
798         if (nfd == -1)
799                 return;
800
801         if (!ptest || (ptest == 1)) {
802                 /*
803                  * Test 1. ulen > packet
804                  */
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);
811                         printf("%d\r", i);
812                         fflush(stdout);
813                         PAUSE();
814                 }
815                 putchar('\n');
816         }
817
818         if (!ptest || (ptest == 2)) {
819                 /*
820                  * Test 2. ulen < packet
821                  */
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--) {
826                         ip->ip_len = i;
827                         (void) send_udp(nfd, 1500, ip, gwip);
828                         printf("%d\r", i);
829                         fflush(stdout);
830                         PAUSE();
831                 }
832                 putchar('\n');
833         }
834
835         if (!ptest || (ptest == 3)) {
836                 /*
837                  * Test 3: sport = 0, sport = 1, sport = 32767
838                  *         sport = 32768, sport = 65535
839                  */
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");
843                 u->uh_sport = 0;
844                 (void) send_udp(nfd, 1500, ip, gwip);
845                 printf("0\n");
846                 fflush(stdout);
847                 PAUSE();
848                 printf("4.3.2 UDP sport = 1\n");
849                 u->uh_sport = htons(1);
850                 (void) send_udp(nfd, 1500, ip, gwip);
851                 printf("1\n");
852                 fflush(stdout);
853                 PAUSE();
854                 printf("4.3.3 UDP sport = 32767\n");
855                 u->uh_sport = htons(32767);
856                 (void) send_udp(nfd, 1500, ip, gwip);
857                 printf("32767\n");
858                 fflush(stdout);
859                 PAUSE();
860                 printf("4.3.4 UDP sport = 32768\n");
861                 u->uh_sport = htons(32768);
862                 (void) send_udp(nfd, 1500, ip, gwip);
863                 printf("32768\n");
864                 putchar('\n');
865                 fflush(stdout);
866                 PAUSE();
867                 printf("4.3.5 UDP sport = 65535\n");
868                 u->uh_sport = htons(65535);
869                 (void) send_udp(nfd, 1500, ip, gwip);
870                 printf("65535\n");
871                 fflush(stdout);
872                 PAUSE();
873         }
874
875         if (!ptest || (ptest == 4)) {
876                 /*
877                  * Test 4: dport = 0, dport = 1, dport = 32767
878                  *         dport = 32768, dport = 65535
879                  */
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");
884                 u->uh_dport = 0;
885                 (void) send_udp(nfd, 1500, ip, gwip);
886                 printf("0\n");
887                 fflush(stdout);
888                 PAUSE();
889                 printf("4.4.2 UDP dport = 1\n");
890                 u->uh_dport = htons(1);
891                 (void) send_udp(nfd, 1500, ip, gwip);
892                 printf("1\n");
893                 fflush(stdout);
894                 PAUSE();
895                 printf("4.4.3 UDP dport = 32767\n");
896                 u->uh_dport = htons(32767);
897                 (void) send_udp(nfd, 1500, ip, gwip);
898                 printf("32767\n");
899                 fflush(stdout);
900                 PAUSE();
901                 printf("4.4.4 UDP dport = 32768\n");
902                 u->uh_dport = htons(32768);
903                 (void) send_udp(nfd, 1500, ip, gwip);
904                 printf("32768\n");
905                 fflush(stdout);
906                 PAUSE();
907                 printf("4.4.5 UDP dport = 65535\n");
908                 u->uh_dport = htons(65535);
909                 (void) send_udp(nfd, 1500, ip, gwip);
910                 printf("65535\n");
911                 fflush(stdout);
912                 PAUSE();
913         }
914
915         if (!ptest || (ptest == 5)) {
916                 /*
917                  * Test 5: sizeof(ip_t) <= MTU <= sizeof(udphdr_t) +
918                  * sizeof(ip_t)
919                  */
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);
923                         printf("%d\r", i);
924                         fflush(stdout);
925                         PAUSE();
926                 }
927                 putchar('\n');
928         }
929 }
930
931
932 /* Perform test 5 (TCP) */
933
934 void    ip_test5(dev, mtu, ip, gwip, ptest)
935 char    *dev;
936 int     mtu;
937 ip_t    *ip;
938 struct  in_addr gwip;
939 int     ptest;
940 {
941 #ifdef USE_NANOSLEEP
942         struct  timespec ts;
943 #else
944         struct  timeval tv;
945 #endif
946         tcphdr_t *t;
947         int     nfd, i;
948
949         t = (tcphdr_t *)((char *)ip + (IP_HL(ip) << 2));
950 #if !defined(linux) && !defined(__osf__)
951         t->th_x2 = 0;
952 #endif
953         TCP_OFF_A(t, 0);
954         t->th_sport = htons(1);
955         t->th_dport = htons(1);
956         t->th_win = htons(4096);
957         t->th_urp = 0;
958         t->th_sum = 0;
959         t->th_seq = htonl(1);
960         t->th_ack = 0;
961         ip->ip_len = sizeof(ip_t) + sizeof(tcphdr_t);
962
963         nfd = initdevice(dev, 1);
964         if (nfd == -1)
965                 return;
966
967         if (!ptest || (ptest == 1)) {
968                 /*
969                  * Test 1: flags variations, 0 - 3f
970                  */
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);
974                      i++) {
975                         t->th_flags = i;
976                         (void) send_tcp(nfd, mtu, ip, gwip);
977                         printf("%d\r", i);
978                         fflush(stdout);
979                         PAUSE();
980                 }
981                 putchar('\n');
982         }
983
984         if (!ptest || (ptest == 2)) {
985                 t->th_flags = TH_SYN;
986                 /*
987                  * Test 2: seq = 0, seq = 1, seq = 0x7fffffff, seq=0x80000000,
988                  *         seq = 0xa000000, seq = 0xffffffff
989                  */
990                 printf("5.2.1 TCP seq = 0\n");
991                 t->th_seq = htonl(0);
992                 (void) send_tcp(nfd, mtu, ip, gwip);
993                 fflush(stdout);
994                 PAUSE();
995
996                 printf("5.2.2 TCP seq = 1\n");
997                 t->th_seq = htonl(1);
998                 (void) send_tcp(nfd, mtu, ip, gwip);
999                 fflush(stdout);
1000                 PAUSE();
1001
1002                 printf("5.2.3 TCP seq = 0x7fffffff\n");
1003                 t->th_seq = htonl(0x7fffffff);
1004                 (void) send_tcp(nfd, mtu, ip, gwip);
1005                 fflush(stdout);
1006                 PAUSE();
1007
1008                 printf("5.2.4 TCP seq = 0x80000000\n");
1009                 t->th_seq = htonl(0x80000000);
1010                 (void) send_tcp(nfd, mtu, ip, gwip);
1011                 fflush(stdout);
1012                 PAUSE();
1013
1014                 printf("5.2.5 TCP seq = 0xc0000000\n");
1015                 t->th_seq = htonl(0xc0000000);
1016                 (void) send_tcp(nfd, mtu, ip, gwip);
1017                 fflush(stdout);
1018                 PAUSE();
1019
1020                 printf("5.2.6 TCP seq = 0xffffffff\n");
1021                 t->th_seq = htonl(0xffffffff);
1022                 (void) send_tcp(nfd, mtu, ip, gwip);
1023                 fflush(stdout);
1024                 PAUSE();
1025         }
1026
1027         if (!ptest || (ptest == 3)) {
1028                 t->th_flags = TH_ACK;
1029                 /*
1030                  * Test 3: ack = 0, ack = 1, ack = 0x7fffffff, ack = 0x8000000
1031                  *         ack = 0xa000000, ack = 0xffffffff
1032                  */
1033                 printf("5.3.1 TCP ack = 0\n");
1034                 t->th_ack = 0;
1035                 (void) send_tcp(nfd, mtu, ip, gwip);
1036                 fflush(stdout);
1037                 PAUSE();
1038
1039                 printf("5.3.2 TCP ack = 1\n");
1040                 t->th_ack = htonl(1);
1041                 (void) send_tcp(nfd, mtu, ip, gwip);
1042                 fflush(stdout);
1043                 PAUSE();
1044
1045                 printf("5.3.3 TCP ack = 0x7fffffff\n");
1046                 t->th_ack = htonl(0x7fffffff);
1047                 (void) send_tcp(nfd, mtu, ip, gwip);
1048                 fflush(stdout);
1049                 PAUSE();
1050
1051                 printf("5.3.4 TCP ack = 0x80000000\n");
1052                 t->th_ack = htonl(0x80000000);
1053                 (void) send_tcp(nfd, mtu, ip, gwip);
1054                 fflush(stdout);
1055                 PAUSE();
1056
1057                 printf("5.3.5 TCP ack = 0xc0000000\n");
1058                 t->th_ack = htonl(0xc0000000);
1059                 (void) send_tcp(nfd, mtu, ip, gwip);
1060                 fflush(stdout);
1061                 PAUSE();
1062
1063                 printf("5.3.6 TCP ack = 0xffffffff\n");
1064                 t->th_ack = htonl(0xffffffff);
1065                 (void) send_tcp(nfd, mtu, ip, gwip);
1066                 fflush(stdout);
1067                 PAUSE();
1068         }
1069
1070         if (!ptest || (ptest == 4)) {
1071                 t->th_flags = TH_SYN;
1072                 /*
1073                  * Test 4: win = 0, win = 32768, win = 65535
1074                  */
1075                 printf("5.4.1 TCP win = 0\n");
1076                 t->th_seq = htonl(0);
1077                 (void) send_tcp(nfd, mtu, ip, gwip);
1078                 fflush(stdout);
1079                 PAUSE();
1080
1081                 printf("5.4.2 TCP win = 32768\n");
1082                 t->th_seq = htonl(0x7fff);
1083                 (void) send_tcp(nfd, mtu, ip, gwip);
1084                 fflush(stdout);
1085                 PAUSE();
1086
1087                 printf("5.4.3 TCP win = 65535\n");
1088                 t->th_win = htons(0xffff);
1089                 (void) send_tcp(nfd, mtu, ip, gwip);
1090                 fflush(stdout);
1091                 PAUSE();
1092         }
1093
1094 #if !defined(linux) && !defined(__SVR4) && !defined(__svr4__) && \
1095     !defined(__sgi) && !defined(__hpux) && !defined(__osf__)
1096         {
1097         struct tcpcb *tcbp, tcb;
1098         struct tcpiphdr ti;
1099         struct sockaddr_in sin;
1100         int fd, slen;
1101
1102         bzero((char *)&sin, sizeof(sin));
1103
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)))
1111                         break;
1112                 close(fd);
1113         }
1114
1115         if (i == 63) {
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;
1120         }
1121
1122         bcopy((char *)ip, (char *)&ti, sizeof(*ip));
1123         t->th_dport = htons(i);
1124         slen = sizeof(sin);
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;
1130         }
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;
1135
1136         if (!ptest || (ptest == 5)) {
1137                 /*
1138                  * Test 5: urp
1139                  */
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);
1145                 PAUSE();
1146
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);
1151                 PAUSE();
1152                 t->th_urp = htons(0x8000);
1153                 (void) send_tcp(nfd, mtu, ip, gwip);
1154                 PAUSE();
1155                 t->th_urp = htons(0xffff);
1156                 (void) send_tcp(nfd, mtu, ip, gwip);
1157                 PAUSE();
1158                 t->th_urp = 0;
1159                 t->th_flags &= ~TH_URG;
1160                 ip->ip_len = sizeof(ip_t) + sizeof(tcphdr_t);
1161         }
1162
1163         if (!ptest || (ptest == 6)) {
1164                 /*
1165                  * Test 6: data offset, off = 0, off is inside, off is outside
1166                  */
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);
1172                         printf("%d\r", i);
1173                         fflush(stdout);
1174                         PAUSE();
1175                 }
1176                 putchar('\n');
1177                 ip->ip_len = sizeof(ip_t) + sizeof(tcphdr_t);
1178         }
1179
1180         (void) close(fd);
1181         }
1182 skip_five_and_six:
1183 #endif
1184         t->th_seq = htonl(1);
1185         t->th_ack = htonl(1);
1186         TCP_OFF_A(t, 0);
1187
1188         if (!ptest || (ptest == 7)) {
1189                 t->th_flags = TH_SYN;
1190                 /*
1191                  * Test 7: sport = 0, sport = 1, sport = 32767
1192                  *         sport = 32768, sport = 65535
1193                  */
1194                 printf("5.7.1 TCP sport = 0\n");
1195                 t->th_sport = 0;
1196                 (void) send_tcp(nfd, mtu, ip, gwip);
1197                 fflush(stdout);
1198                 PAUSE();
1199
1200                 printf("5.7.2 TCP sport = 1\n");
1201                 t->th_sport = htons(1);
1202                 (void) send_tcp(nfd, mtu, ip, gwip);
1203                 fflush(stdout);
1204                 PAUSE();
1205
1206                 printf("5.7.3 TCP sport = 32767\n");
1207                 t->th_sport = htons(32767);
1208                 (void) send_tcp(nfd, mtu, ip, gwip);
1209                 fflush(stdout);
1210                 PAUSE();
1211
1212                 printf("5.7.4 TCP sport = 32768\n");
1213                 t->th_sport = htons(32768);
1214                 (void) send_tcp(nfd, mtu, ip, gwip);
1215                 fflush(stdout);
1216                 PAUSE();
1217
1218                 printf("5.7.5 TCP sport = 65535\n");
1219                 t->th_sport = htons(65535);
1220                 (void) send_tcp(nfd, mtu, ip, gwip);
1221                 fflush(stdout);
1222                 PAUSE();
1223         }
1224
1225         if (!ptest || (ptest == 8)) {
1226                 t->th_sport = htons(1);
1227                 t->th_flags = TH_SYN;
1228                 /*
1229                  * Test 8: dport = 0, dport = 1, dport = 32767
1230                  *         dport = 32768, dport = 65535
1231                  */
1232                 printf("5.8.1 TCP dport = 0\n");
1233                 t->th_dport = 0;
1234                 (void) send_tcp(nfd, mtu, ip, gwip);
1235                 fflush(stdout);
1236                 PAUSE();
1237
1238                 printf("5.8.2 TCP dport = 1\n");
1239                 t->th_dport = htons(1);
1240                 (void) send_tcp(nfd, mtu, ip, gwip);
1241                 fflush(stdout);
1242                 PAUSE();
1243
1244                 printf("5.8.3 TCP dport = 32767\n");
1245                 t->th_dport = htons(32767);
1246                 (void) send_tcp(nfd, mtu, ip, gwip);
1247                 fflush(stdout);
1248                 PAUSE();
1249
1250                 printf("5.8.4 TCP dport = 32768\n");
1251                 t->th_dport = htons(32768);
1252                 (void) send_tcp(nfd, mtu, ip, gwip);
1253                 fflush(stdout);
1254                 PAUSE();
1255
1256                 printf("5.8.5 TCP dport = 65535\n");
1257                 t->th_dport = htons(65535);
1258                 (void) send_tcp(nfd, mtu, ip, gwip);
1259                 fflush(stdout);
1260                 PAUSE();
1261         }
1262
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);
1272                 fflush(stdout);
1273                 PAUSE();
1274         }
1275
1276         /* TCP options header checking */
1277         /* 0 length options, etc */
1278 }
1279
1280
1281 /* Perform test 6 (exhaust mbuf test) */
1282
1283 void    ip_test6(dev, mtu, ip, gwip, ptest)
1284 char    *dev;
1285 int     mtu;
1286 ip_t    *ip;
1287 struct  in_addr gwip;
1288 int     ptest;
1289 {
1290 #ifdef USE_NANOSLEEP
1291         struct  timespec ts;
1292 #else
1293         struct  timeval tv;
1294 #endif
1295         udphdr_t *u;
1296         int     nfd, i, j, k;
1297
1298         IP_V_A(ip, IPVERSION);
1299         ip->ip_tos = 0;
1300         ip->ip_off = 0;
1301         ip->ip_ttl = 60;
1302         ip->ip_p = IPPROTO_UDP;
1303         ip->ip_sum = 0;
1304         u = (udphdr_t *)(ip + 1);
1305         u->uh_sport = htons(1);
1306         u->uh_dport = htons(9);
1307         u->uh_sum = 0;
1308
1309         nfd = initdevice(dev, 1);
1310         if (nfd == -1)
1311                 return;
1312
1313         u->uh_ulen = htons(7168);
1314
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++) {
1319                 /*
1320                  * First send the entire packet in 768 byte chunks.
1321                  */
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);
1327                 fflush(stdout);
1328                 PAUSE();
1329                 /*
1330                  * And again using 128 byte chunks.
1331                  */
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);
1336                 fflush(stdout);
1337                 PAUSE();
1338
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);
1344                         fflush(stdout);
1345                         PAUSE();
1346
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);
1352                                 fflush(stdout);
1353                                 PAUSE();
1354                         }
1355                 }
1356         }
1357         putchar('\n');
1358 }
1359
1360
1361 /* Perform test 7 (random packets) */
1362
1363 static  u_long  tbuf[64];
1364
1365 void    ip_test7(dev, mtu, ip, gwip, ptest)
1366 char    *dev;
1367 int     mtu;
1368 ip_t    *ip;
1369 struct  in_addr gwip;
1370 int     ptest;
1371 {
1372         ip_t    *pip;
1373 #ifdef USE_NANOSLEEP
1374         struct  timespec ts;
1375 #else
1376         struct  timeval tv;
1377 #endif
1378         int     nfd, i, j;
1379         u_char  *s;
1380
1381         nfd = initdevice(dev, 1);
1382         if (nfd == -1)
1383                 return;
1384
1385         pip = (ip_t *)tbuf;
1386
1387         srand(time(NULL) ^ (getpid() * getppid()));
1388
1389         printf("7. send 1024 random IP packets.\n");
1390
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));
1397                 pip->ip_sum = 0;
1398                 pip->ip_len &= 0xff;
1399                 (void) send_ip(nfd, mtu, pip, gwip, 0);
1400                 printf("%d\r", i);
1401                 fflush(stdout);
1402                 PAUSE();
1403         }
1404         putchar('\n');
1405
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));
1413                 pip->ip_sum = 0;
1414                 pip->ip_len &= 0xff;
1415                 (void) send_ip(nfd, mtu, pip, gwip, 0);
1416                 printf("%d\r", i);
1417                 fflush(stdout);
1418                 PAUSE();
1419         }
1420         putchar('\n');
1421 }