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