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