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