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