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