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