iptests.c revision 110920
12061Sjkh/*
250479Speter * Copyright (C) 1993-1998 by Darren Reed.
32061Sjkh *
438666Sjb * See the IPFILTER.LICENCE file for details on licencing.
532427Sjb */
6111131Sru#if defined(__sgi) && (IRIX > 602)
7111131Sru# include <sys/ptimers.h>
838666Sjb#endif
938666Sjb#include <stdio.h>
1038666Sjb#include <unistd.h>
1138666Sjb#include <stdlib.h>
1264049Salex#include <string.h>
1364049Salex#include <sys/types.h>
14116679Ssimokawa#include <sys/time.h>
1566071Smarkm#include <sys/param.h>
16116679Ssimokawa#define _KERNEL
1773504Sobrien#define KERNEL
1838666Sjb#if !defined(solaris) && !defined(linux) && !defined(__sgi)
1932427Sjb# include <sys/file.h>
2038666Sjb#else
21108451Sschweikh# ifdef solaris
2238666Sjb#  include <sys/dditypes.h>
2338666Sjb# endif
2438666Sjb#endif
2538666Sjb#undef  _KERNEL
2617308Speter#undef  KERNEL
2791606Skeramida#if !defined(solaris) && !defined(linux) && !defined(__sgi)
2819175Sbde# include <nlist.h>
2996205Sjwd# include <sys/user.h>
3096205Sjwd# include <sys/proc.h>
3138042Sbde#endif
3296205Sjwd#if !defined(ultrix) && !defined(hpux) && !defined(linux) && !defined(__sgi)
3396205Sjwd# include <kvm.h>
3438042Sbde#endif
3596205Sjwd#ifndef	ultrix
3696205Sjwd# include <sys/socket.h>
3717308Speter#endif
3896205Sjwd#if defined(solaris)
3996205Sjwd# include <sys/stream.h>
4017308Speter#endif
4196205Sjwd#include <sys/socketvar.h>
4296205Sjwd#ifdef sun
4396205Sjwd#include <sys/systm.h>
4496205Sjwd#include <sys/session.h>
4596205Sjwd#endif
4696205Sjwd#if BSD >= 199103
4796205Sjwd# include <sys/sysctl.h>
4896205Sjwd# include <sys/filedesc.h>
4996205Sjwd# include <paths.h>
5096205Sjwd#endif
5196205Sjwd#include <netinet/in_systm.h>
5296205Sjwd#include <sys/socket.h>
5398775Sdillon#include <net/if.h>
5498723Sdillon#if defined(linux) && (LINUX >= 0200)
5598723Sdillon# include <asm/atomic.h>
5698723Sdillon#endif
5798723Sdillon#if !defined(linux)
5838666Sjb# include <net/route.h>
5938666Sjb#else
6017308Speter# define __KERNEL__	/* because there's a macro not wrapped by this */
61123311Speter# include <net/route.h>	/* in this file :-/ */
62123311Speter#endif
63123311Speter#include <netinet/in.h>
64123311Speter#include <arpa/inet.h>
6595509Sru#include <netinet/ip.h>
6695793Sru#include <netinet/tcp.h>
67116679Ssimokawa#include <netinet/udp.h>
68120760Sru#include <netinet/ip_icmp.h>
69128189Sdes#ifndef linux
70127360Sru# include <netinet/ip_var.h>
71123311Speter# include <netinet/in_pcb.h>
72123311Speter# include <netinet/tcp_timer.h>
732061Sjkh# include <netinet/tcp_var.h>
7497769Sru#endif
7597252Sru#if defined(__SVR4) || defined(__svr4__) || defined(__sgi)
76119579Sru# include <sys/sysmacros.h>
7797252Sru#endif
7895730Sru#if defined(__NetBSD_Version__) && (__NetBSD_Version__ >= 106000000)
7995793Sru# define USE_NANOSLEEP
80111617Sru#endif
8195730Sru#include "ipsend.h"
82116679Ssimokawa
8395730Sru#if 0
84116679Ssimokawa#if !defined(lint)
8595730Srustatic const char sccsid[] = "%W% %G% (C)1995 Darren Reed";
86110035Srustatic const char rcsid[] = "@(#)$Id: iptests.c,v 2.1.4.8 2002/12/06 11:40:35 darrenr Exp $";
87107516Sru#endif
88110035Sru#endif
89117234Sru
90110035Sru__FBSDID("$FreeBSD: head/contrib/ipfilter/ipsend/iptests.c 110920 2003-02-15 06:32:48Z darrenr $");
91117229Sru
92117234Sru
9354324Smarcel#ifdef	USE_NANOSLEEP
9417308Speter# define	PAUSE()	ts.tv_sec = 0; ts.tv_nsec = 10000000; \
95119519Smarcel		  (void) nanosleep(&ts, NULL)
96119519Smarcel#else
97119519Smarcel# define	PAUSE()	tv.tv_sec = 0; tv.tv_usec = 10000; \
98119519Smarcel				  (void) select(0, NULL, NULL, NULL, &tv)
99119519Smarcel#endif
100119519Smarcel
101119579Sru
102119519Smarcelvoid	ip_test1(dev, mtu, ip, gwip, ptest)
103119519Smarcelchar	*dev;
104119519Smarcelint	mtu;
105119519Smarcelip_t	*ip;
106119519Smarcelstruct	in_addr	gwip;
107126031Sgadint	ptest;
108126024Sgad{
109126024Sgad#ifdef	USE_NANOSLEEP
110126024Sgad	struct	timespec ts;
111126024Sgad#else
112126024Sgad	struct	timeval	tv;
113126024Sgad#endif
114126024Sgad	udphdr_t *u;
115126024Sgad	int	nfd, i = 0, len, id = getpid();
116126024Sgad
117126024Sgad	ip->ip_hl = sizeof(*ip) >> 2;
118126024Sgad	ip->ip_v = IPVERSION;
119126024Sgad	ip->ip_tos = 0;
120126024Sgad	ip->ip_off = 0;
121126031Sgad	ip->ip_ttl = 60;
122126024Sgad	ip->ip_p = IPPROTO_UDP;
123126024Sgad	ip->ip_sum = 0;
124126024Sgad	u = (udphdr_t *)(ip + 1);
125126024Sgad	u->uh_sport = htons(1);
126126024Sgad	u->uh_dport = htons(9);
127126024Sgad	u->uh_sum = 0;
128126024Sgad	u->uh_ulen = htons(sizeof(*u) + 4);
129126024Sgad	ip->ip_len = sizeof(*ip) + ntohs(u->uh_ulen);
130126024Sgad	len = ip->ip_len;
131126024Sgad	nfd = initdevice(dev, u->uh_sport, 1);
132126024Sgad
133126024Sgad	if (!ptest || (ptest == 1)) {
134126024Sgad		/*
135125885Sgad		 * Part1: hl < len
136125885Sgad		 */
13738666Sjb		ip->ip_id = 0;
13817308Speter		printf("1.1. sending packets with ip_hl < ip_len\n");
139119519Smarcel		for (i = 0; i < ((sizeof(*ip) + ntohs(u->uh_ulen)) >> 2); i++) {
140119579Sru			ip->ip_hl = i >> 2;
14138666Sjb			(void) send_ip(nfd, 1500, ip, gwip, 1);
142110035Sru			printf("%d\r", i);
1432302Spaul			fflush(stdout);
14439206Sjkh			PAUSE();
14539206Sjkh		}
14639206Sjkh		putchar('\n');
147126485Sru	}
14873349Sru
149126485Sru	if (!ptest || (ptest == 2)) {
15017308Speter		/*
15154324Smarcel		 * Part2: hl > len
15254324Smarcel		 */
15354324Smarcel		ip->ip_id = 0;
15454324Smarcel		printf("1.2. sending packets with ip_hl > ip_len\n");
15554324Smarcel		for (; i < ((sizeof(*ip) * 2 + ntohs(u->uh_ulen)) >> 2); i++) {
15654324Smarcel			ip->ip_hl = i >> 2;
15754324Smarcel			(void) send_ip(nfd, 1500, ip, gwip, 1);
158118531Sru			printf("%d\r", i);
15954324Smarcel			fflush(stdout);
16054324Smarcel			PAUSE();
16154324Smarcel		}
16254324Smarcel		putchar('\n');
16354324Smarcel	}
16454324Smarcel
165110035Sru	if (!ptest || (ptest == 3)) {
16654324Smarcel		/*
167110035Sru		 * Part3: v < 4
168110035Sru		 */
16954324Smarcel		ip->ip_id = 0;
17054324Smarcel		printf("1.3. ip_v < 4\n");
17154324Smarcel		ip->ip_hl = sizeof(*ip) >> 2;
17254324Smarcel		for (i = 0; i < 4; i++) {
17354324Smarcel			ip->ip_v = i;
174110035Sru			(void) send_ip(nfd, 1500, ip, gwip, 1);
17554324Smarcel			printf("%d\r", i);
17654324Smarcel			fflush(stdout);
17754324Smarcel			PAUSE();
178118531Sru		}
179118531Sru		putchar('\n');
18054324Smarcel	}
18154324Smarcel
18254324Smarcel	if (!ptest || (ptest == 4)) {
18395730Sru		/*
18495730Sru		 * Part4: v > 4
18595730Sru		 */
18695730Sru		ip->ip_id = 0;
18795730Sru		printf("1.4. ip_v > 4\n");
18895730Sru		for (i = 5; i < 16; i++) {
18995730Sru			ip->ip_v = i;
19038666Sjb			(void) send_ip(nfd, 1500, ip, gwip, 1);
191107374Sru			printf("%d\r", i);
19217308Speter			fflush(stdout);
19355678Smarcel			PAUSE();
194110035Sru		}
195117793Sru		putchar('\n');
196110035Sru	}
197110035Sru
198110035Sru	if (!ptest || (ptest == 5)) {
1992061Sjkh		/*
20017308Speter		 * Part5: len < packet
201107516Sru		 */
202107374Sru		ip->ip_id = 0;
20355678Smarcel		ip->ip_v = IPVERSION;
204107516Sru		i = ip->ip_len + 1;
205107516Sru		printf("1.5.0 ip_len < packet size (size++, long packets)\n");
206107516Sru		for (; i < (ip->ip_len * 2); i++) {
207107516Sru			ip->ip_id = htons(id++);
208107516Sru			ip->ip_sum = 0;
209107516Sru			ip->ip_sum = chksum((u_short *)ip, ip->ip_hl << 2);
210107516Sru			(void) send_ether(nfd, (char *)ip, i, gwip);
211107516Sru			printf("%d\r", i);
212122204Skris			fflush(stdout);
21355678Smarcel			PAUSE();
21455678Smarcel		}
215116696Sru		putchar('\n');
21655678Smarcel		printf("1.5.1 ip_len < packet size (ip_len-, short packets)\n");
21755678Smarcel		for (i = len; i > 0; i--) {
218107516Sru			ip->ip_id = htons(id++);
219107516Sru			ip->ip_len = i;
220107516Sru			ip->ip_sum = 0;
221107516Sru			ip->ip_sum = chksum((u_short *)ip, ip->ip_hl << 2);
22255678Smarcel			(void) send_ether(nfd, (char *)ip, len, gwip);
22355678Smarcel			printf("%d\r", i);
224111131Sru			fflush(stdout);
225111131Sru			PAUSE();
226111131Sru		}
227111131Sru		putchar('\n');
228111131Sru	}
229111131Sru
230111131Sru	if (!ptest || (ptest == 6)) {
231103985Sphk		/*
232103985Sphk		 * Part6: len > packet
233103985Sphk		 */
234103985Sphk		ip->ip_id = 0;
235126403Sjohan		printf("1.6.0 ip_len > packet size (increase ip_len)\n");
236111131Sru		for (i = len + 1; i < (len * 2); i++) {
237111131Sru			ip->ip_id = htons(id++);
238111131Sru			ip->ip_len = i;
239111131Sru			ip->ip_sum = 0;
240111131Sru			ip->ip_sum = chksum((u_short *)ip, ip->ip_hl << 2);
241111131Sru			(void) send_ether(nfd, (char *)ip, len, gwip);
242111131Sru			printf("%d\r", i);
243111131Sru			fflush(stdout);
244111131Sru			PAUSE();
245111133Sru		}
246103985Sphk		putchar('\n');
247111131Sru		ip->ip_len = len;
248111131Sru		printf("1.6.1 ip_len > packet size (size--, short packets)\n");
249103985Sphk		for (i = len; i > 0; i--) {
250111131Sru			ip->ip_id = htons(id++);
251103985Sphk			ip->ip_sum = 0;
252118531Sru			ip->ip_sum = chksum((u_short *)ip, ip->ip_hl << 2);
253118531Sru			(void) send_ether(nfd, (char *)ip, i, gwip);
254103985Sphk			printf("%d\r", i);
255103985Sphk			fflush(stdout);
256126485Sru			PAUSE();
257111131Sru		}
258111131Sru		putchar('\n');
259126485Sru	}
260103985Sphk
261103985Sphk	if (!ptest || (ptest == 7)) {
262111131Sru		/*
263111131Sru		 * Part7: 0 length fragment
264111131Sru		 */
265111131Sru		printf("1.7.0 Zero length fragments (ip_off = 0x2000)\n");
266111131Sru		ip->ip_id = 0;
267103985Sphk		ip->ip_len = sizeof(*ip);
268		ip->ip_off = htons(IP_MF);
269		(void) send_ip(nfd, mtu, ip, gwip, 1);
270		fflush(stdout);
271		PAUSE();
272
273		printf("1.7.1 Zero length fragments (ip_off = 0x3000)\n");
274		ip->ip_id = 0;
275		ip->ip_len = sizeof(*ip);
276		ip->ip_off = htons(IP_MF);
277		(void) send_ip(nfd, mtu, ip, gwip, 1);
278		fflush(stdout);
279		PAUSE();
280
281		printf("1.7.2 Zero length fragments (ip_off = 0xa000)\n");
282		ip->ip_id = 0;
283		ip->ip_len = sizeof(*ip);
284		ip->ip_off = htons(0xa000);
285		(void) send_ip(nfd, mtu, ip, gwip, 1);
286		fflush(stdout);
287		PAUSE();
288
289		printf("1.7.3 Zero length fragments (ip_off = 0x0100)\n");
290		ip->ip_id = 0;
291		ip->ip_len = sizeof(*ip);
292		ip->ip_off = htons(0x0100);
293		(void) send_ip(nfd, mtu, ip, gwip, 1);
294		fflush(stdout);
295		PAUSE();
296	}
297
298	if (!ptest || (ptest == 8)) {
299		struct	timeval	tv;
300
301		gettimeofday(&tv, NULL);
302		srand(tv.tv_sec ^ getpid() ^ tv.tv_usec);
303		/*
304		 * Part8.1: 63k packet + 1k fragment at offset 0x1ffe
305		 * Mark it as being ICMP (so it doesn't get junked), but
306		 * don't bother about the ICMP header, we're not worrying
307		 * about that here.
308		 */
309		ip->ip_p = IPPROTO_ICMP;
310		ip->ip_off = htons(IP_MF);
311		u->uh_dport = htons(9);
312		ip->ip_id = htons(id++);
313		printf("1.8.1 63k packet + 1k fragment at offset 0x1ffe\n");
314		ip->ip_len = 768 + 20 + 8;
315		(void) send_ip(nfd, mtu, ip, gwip, 1);
316		printf("%d\r", i);
317
318		ip->ip_len = MIN(768 + 20, mtu - 68);
319		i = 512;
320		for (; i < (63 * 1024 + 768); i += 768) {
321			ip->ip_off = htons(IP_MF | ((i >> 3) & 0x1fff));
322			(void) send_ip(nfd, mtu, ip, gwip, 1);
323			printf("%d\r", i);
324			fflush(stdout);
325			PAUSE();
326		}
327		ip->ip_len = 896 + 20;
328		ip->ip_off = htons((i >> 3) & 0x1fff);
329		(void) send_ip(nfd, mtu, ip, gwip, 1);
330		printf("%d\r", i);
331		putchar('\n');
332		fflush(stdout);
333
334		/*
335		 * Part8.2: 63k packet + 1k fragment at offset 0x1ffe
336		 * Mark it as being ICMP (so it doesn't get junked), but
337		 * don't bother about the ICMP header, we're not worrying
338		 * about that here.  (Lossage here)
339		 */
340		ip->ip_p = IPPROTO_ICMP;
341		ip->ip_off = htons(IP_MF);
342		u->uh_dport = htons(9);
343		ip->ip_id = htons(id++);
344		printf("1.8.2 63k packet + 1k fragment at offset 0x1ffe\n");
345		ip->ip_len = 768 + 20 + 8;
346		if ((rand() & 0x1f) != 0) {
347			(void) send_ip(nfd, mtu, ip, gwip, 1);
348			printf("%d\r", i);
349		} else
350			printf("skip 0\n");
351
352		ip->ip_len = MIN(768 + 20, mtu - 68);
353		i = 512;
354		for (; i < (63 * 1024 + 768); i += 768) {
355			ip->ip_off = htons(IP_MF | ((i >> 3) & 0x1fff));
356			if ((rand() & 0x1f) != 0) {
357				(void) send_ip(nfd, mtu, ip, gwip, 1);
358				printf("%d\r", i);
359			} else
360				printf("skip %d\n", i);
361			fflush(stdout);
362			PAUSE();
363		}
364		ip->ip_len = 896 + 20;
365		ip->ip_off = htons((i >> 3) & 0x1fff);
366		if ((rand() & 0x1f) != 0) {
367			(void) send_ip(nfd, mtu, ip, gwip, 1);
368			printf("%d\r", i);
369		} else
370			printf("skip\n");
371		putchar('\n');
372		fflush(stdout);
373
374		/*
375		 * Part8.3: 33k packet - test for not dealing with -ve length
376		 * Mark it as being ICMP (so it doesn't get junked), but
377		 * don't bother about the ICMP header, we're not worrying
378		 * about that here.
379		 */
380		ip->ip_p = IPPROTO_ICMP;
381		ip->ip_off = htons(IP_MF);
382		u->uh_dport = htons(9);
383		ip->ip_id = htons(id++);
384		printf("1.8.3 33k packet\n");
385		ip->ip_len = 768 + 20 + 8;
386		(void) send_ip(nfd, mtu, ip, gwip, 1);
387		printf("%d\r", i);
388
389		ip->ip_len = MIN(768 + 20, mtu - 68);
390		i = 512;
391		for (; i < (32 * 1024 + 768); i += 768) {
392			ip->ip_off = htons(IP_MF | ((i >> 3) & 0x1fff));
393			(void) send_ip(nfd, mtu, ip, gwip, 1);
394			printf("%d\r", i);
395			fflush(stdout);
396			PAUSE();
397		}
398		ip->ip_len = 896 + 20;
399		ip->ip_off = htons((i >> 3) & 0x1fff);
400		(void) send_ip(nfd, mtu, ip, gwip, 1);
401		printf("%d\r", i);
402		putchar('\n');
403		fflush(stdout);
404	}
405
406	ip->ip_len = len;
407	ip->ip_off = 0;
408	if (!ptest || (ptest == 9)) {
409		/*
410		 * Part9: off & 0x8000 == 0x8000
411		 */
412		ip->ip_id = 0;
413		ip->ip_off = htons(0x8000);
414		printf("1.9. ip_off & 0x8000 == 0x8000\n");
415		(void) send_ip(nfd, mtu, ip, gwip, 1);
416		fflush(stdout);
417		PAUSE();
418	}
419
420	ip->ip_off = 0;
421
422	if (!ptest || (ptest == 10)) {
423		/*
424		 * Part10: ttl = 255
425		 */
426		ip->ip_id = 0;
427		ip->ip_ttl = 255;
428		printf("1.10.0 ip_ttl = 255\n");
429		(void) send_ip(nfd, mtu, ip, gwip, 1);
430		fflush(stdout);
431		PAUSE();
432
433		ip->ip_ttl = 128;
434		printf("1.10.1 ip_ttl = 128\n");
435		(void) send_ip(nfd, mtu, ip, gwip, 1);
436		fflush(stdout);
437		PAUSE();
438
439		ip->ip_ttl = 0;
440		printf("1.10.2 ip_ttl = 0\n");
441		(void) send_ip(nfd, mtu, ip, gwip, 1);
442		fflush(stdout);
443		PAUSE();
444	}
445
446	(void) close(nfd);
447}
448
449
450void	ip_test2(dev, mtu, ip, gwip, ptest)
451char	*dev;
452int	mtu;
453ip_t	*ip;
454struct	in_addr	gwip;
455int	ptest;
456{
457#ifdef	USE_NANOSLEEP
458	struct	timespec ts;
459#else
460	struct	timeval	tv;
461#endif
462	int	nfd;
463	u_char	*s;
464
465	s = (u_char *)(ip + 1);
466	nfd = initdevice(dev, htons(1), 1);
467
468	ip->ip_hl = 6;
469	ip->ip_len = ip->ip_hl << 2;
470	s[IPOPT_OPTVAL] = IPOPT_NOP;
471	s++;
472	if (!ptest || (ptest == 1)) {
473		/*
474		 * Test 1: option length > packet length,
475		 *                header length == packet length
476		 */
477		s[IPOPT_OPTVAL] = IPOPT_TS;
478		s[IPOPT_OLEN] = 4;
479		s[IPOPT_OFFSET] = IPOPT_MINOFF;
480		ip->ip_p = IPPROTO_IP;
481		printf("2.1 option length > packet length\n");
482		(void) send_ip(nfd, mtu, ip, gwip, 1);
483		fflush(stdout);
484		PAUSE();
485	}
486
487	ip->ip_hl = 7;
488	ip->ip_len = ip->ip_hl << 2;
489	if (!ptest || (ptest == 1)) {
490		/*
491		 * Test 2: options have length = 0
492		 */
493		printf("2.2.1 option length = 0, RR\n");
494		s[IPOPT_OPTVAL] = IPOPT_RR;
495		s[IPOPT_OLEN] = 0;
496		(void) send_ip(nfd, mtu, ip, gwip, 1);
497		fflush(stdout);
498		PAUSE();
499
500		printf("2.2.2 option length = 0, TS\n");
501		s[IPOPT_OPTVAL] = IPOPT_TS;
502		s[IPOPT_OLEN] = 0;
503		(void) send_ip(nfd, mtu, ip, gwip, 1);
504		fflush(stdout);
505		PAUSE();
506
507		printf("2.2.3 option length = 0, SECURITY\n");
508		s[IPOPT_OPTVAL] = IPOPT_SECURITY;
509		s[IPOPT_OLEN] = 0;
510		(void) send_ip(nfd, mtu, ip, gwip, 1);
511		fflush(stdout);
512		PAUSE();
513
514		printf("2.2.4 option length = 0, LSRR\n");
515		s[IPOPT_OPTVAL] = IPOPT_LSRR;
516		s[IPOPT_OLEN] = 0;
517		(void) send_ip(nfd, mtu, ip, gwip, 1);
518		fflush(stdout);
519		PAUSE();
520
521		printf("2.2.5 option length = 0, SATID\n");
522		s[IPOPT_OPTVAL] = IPOPT_SATID;
523		s[IPOPT_OLEN] = 0;
524		(void) send_ip(nfd, mtu, ip, gwip, 1);
525		fflush(stdout);
526		PAUSE();
527
528		printf("2.2.6 option length = 0, SSRR\n");
529		s[IPOPT_OPTVAL] = IPOPT_SSRR;
530		s[IPOPT_OLEN] = 0;
531		(void) send_ip(nfd, mtu, ip, gwip, 1);
532		fflush(stdout);
533		PAUSE();
534	}
535
536	(void) close(nfd);
537}
538
539
540/*
541 * test 3 (ICMP)
542 */
543void	ip_test3(dev, mtu, ip, gwip, ptest)
544char	*dev;
545int	mtu;
546ip_t	*ip;
547struct	in_addr	gwip;
548int	ptest;
549{
550	static	int	ict1[10] = { 8, 9, 10, 13, 14, 15, 16, 17, 18, 0 };
551	static	int	ict2[8] = { 3, 9, 10, 13, 14, 17, 18, 0 };
552#ifdef	USE_NANOSLEEP
553	struct	timespec ts;
554#else
555	struct	timeval	tv;
556#endif
557	struct	icmp	*icp;
558	int	nfd, i;
559
560	ip->ip_hl = sizeof(*ip) >> 2;
561	ip->ip_v = IPVERSION;
562	ip->ip_tos = 0;
563	ip->ip_off = 0;
564	ip->ip_ttl = 60;
565	ip->ip_p = IPPROTO_ICMP;
566	ip->ip_sum = 0;
567	ip->ip_len = sizeof(*ip) + sizeof(*icp);
568	icp = (struct icmp *)((char *)ip + (ip->ip_hl << 2));
569	nfd = initdevice(dev, htons(1), 1);
570
571	if (!ptest || (ptest == 1)) {
572		/*
573		 * Type 0 - 31, 255, code = 0
574		 */
575		bzero((char *)icp, sizeof(*icp));
576		for (i = 0; i < 32; i++) {
577			icp->icmp_type = i;
578			(void) send_icmp(nfd, mtu, ip, gwip);
579			PAUSE();
580			printf("3.1.%d ICMP type %d code 0 (all 0's)\r", i, i);
581		}
582		icp->icmp_type = 255;
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, 255);
586		putchar('\n');
587	}
588
589	if (!ptest || (ptest == 2)) {
590		/*
591		 * Type 3, code = 0 - 31
592		 */
593		icp->icmp_type = 3;
594		for (i = 0; i < 32; i++) {
595			icp->icmp_code = i;
596			(void) send_icmp(nfd, mtu, ip, gwip);
597			PAUSE();
598			printf("3.2.%d ICMP type 3 code %d (all 0's)\r", i, i);
599		}
600	}
601
602	if (!ptest || (ptest == 3)) {
603		/*
604		 * Type 4, code = 0,127,128,255
605		 */
606		icp->icmp_type = 4;
607		icp->icmp_code = 0;
608		(void) send_icmp(nfd, mtu, ip, gwip);
609		PAUSE();
610		printf("3.3.1 ICMP type 4 code 0 (all 0's)\r");
611		icp->icmp_code = 127;
612		(void) send_icmp(nfd, mtu, ip, gwip);
613		PAUSE();
614		printf("3.3.2 ICMP type 4 code 127 (all 0's)\r");
615		icp->icmp_code = 128;
616		(void) send_icmp(nfd, mtu, ip, gwip);
617		PAUSE();
618		printf("3.3.3 ICMP type 4 code 128 (all 0's)\r");
619		icp->icmp_code = 255;
620		(void) send_icmp(nfd, mtu, ip, gwip);
621		PAUSE();
622		printf("3.3.4 ICMP type 4 code 255 (all 0's)\r");
623	}
624
625	if (!ptest || (ptest == 4)) {
626		/*
627		 * Type 5, code = 0,127,128,255
628		 */
629		icp->icmp_type = 5;
630		icp->icmp_code = 0;
631		(void) send_icmp(nfd, mtu, ip, gwip);
632		PAUSE();
633		printf("3.4.1 ICMP type 5 code 0 (all 0's)\r");
634		icp->icmp_code = 127;
635		(void) send_icmp(nfd, mtu, ip, gwip);
636		PAUSE();
637		printf("3.4.2 ICMP type 5 code 127 (all 0's)\r");
638		icp->icmp_code = 128;
639		(void) send_icmp(nfd, mtu, ip, gwip);
640		PAUSE();
641		printf("3.4.3 ICMP type 5 code 128 (all 0's)\r");
642		icp->icmp_code = 255;
643		(void) send_icmp(nfd, mtu, ip, gwip);
644		PAUSE();
645		printf("3.4.4 ICMP type 5 code 255 (all 0's)\r");
646	}
647
648	if (!ptest || (ptest == 5)) {
649		/*
650		 * Type 8-10;13-18, code - 0,127,128,255
651		 */
652		for (i = 0; ict1[i]; i++) {
653			icp->icmp_type = ict1[i];
654			icp->icmp_code = 0;
655			(void) send_icmp(nfd, mtu, ip, gwip);
656			PAUSE();
657			printf("3.5.%d ICMP type 5 code 0 (all 0's)\r",
658				i * 4);
659			icp->icmp_code = 127;
660			(void) send_icmp(nfd, mtu, ip, gwip);
661			PAUSE();
662			printf("3.5.%d ICMP type 5 code 127 (all 0's)\r",
663				i * 4 + 1);
664			icp->icmp_code = 128;
665			(void) send_icmp(nfd, mtu, ip, gwip);
666			PAUSE();
667			printf("3.5.%d ICMP type 5 code 128 (all 0's)\r",
668				i * 4 + 2);
669			icp->icmp_code = 255;
670			(void) send_icmp(nfd, mtu, ip, gwip);
671			PAUSE();
672			printf("3.5.%d ICMP type 5 code 255 (all 0's)\r",
673				i * 4 + 3);
674		}
675		putchar('\n');
676	}
677
678	if (!ptest || (ptest == 6)) {
679		/*
680		 * Type 12, code - 0,127,128,129,255
681		 */
682		icp->icmp_type = 12;
683		icp->icmp_code = 0;
684		(void) send_icmp(nfd, mtu, ip, gwip);
685		PAUSE();
686		printf("3.6.1 ICMP type 12 code 0 (all 0's)\r");
687		icp->icmp_code = 127;
688		(void) send_icmp(nfd, mtu, ip, gwip);
689		PAUSE();
690		printf("3.6.2 ICMP type 12 code 127 (all 0's)\r");
691		icp->icmp_code = 128;
692		(void) send_icmp(nfd, mtu, ip, gwip);
693		PAUSE();
694		printf("3.6.3 ICMP type 12 code 128 (all 0's)\r");
695		icp->icmp_code = 129;
696		(void) send_icmp(nfd, mtu, ip, gwip);
697		PAUSE();
698		printf("3.6.4 ICMP type 12 code 129 (all 0's)\r");
699		icp->icmp_code = 255;
700		(void) send_icmp(nfd, mtu, ip, gwip);
701		PAUSE();
702		printf("3.6.5 ICMP type 12 code 255 (all 0's)\r");
703		putchar('\n');
704	}
705
706	if (!ptest || (ptest == 7)) {
707		/*
708		 * Type 3;9-10;13-14;17-18 - shorter packets
709		 */
710		ip->ip_len = sizeof(*ip) + sizeof(*icp) / 2;
711		for (i = 0; ict2[i]; i++) {
712			icp->icmp_type = ict1[i];
713			icp->icmp_code = 0;
714			(void) send_icmp(nfd, mtu, ip, gwip);
715			PAUSE();
716			printf("3.5.%d ICMP type %d code 0 (all 0's)\r",
717				i * 4, icp->icmp_type);
718			icp->icmp_code = 127;
719			(void) send_icmp(nfd, mtu, ip, gwip);
720			PAUSE();
721			printf("3.5.%d ICMP type %d code 127 (all 0's)\r",
722				i * 4 + 1, icp->icmp_type);
723			icp->icmp_code = 128;
724			(void) send_icmp(nfd, mtu, ip, gwip);
725			PAUSE();
726			printf("3.5.%d ICMP type %d code 128 (all 0's)\r",
727				i * 4 + 2, icp->icmp_type);
728			icp->icmp_code = 255;
729			(void) send_icmp(nfd, mtu, ip, gwip);
730			PAUSE();
731			printf("3.5.%d ICMP type %d code 127 (all 0's)\r",
732				i * 4 + 3, icp->icmp_type);
733		}
734		putchar('\n');
735	}
736}
737
738
739/* Perform test 4 (UDP) */
740
741void	ip_test4(dev, mtu, ip, gwip, ptest)
742char	*dev;
743int	mtu;
744ip_t	*ip;
745struct	in_addr	gwip;
746int	ptest;
747{
748#ifdef	USE_NANOSLEEP
749	struct	timespec ts;
750#else
751	struct	timeval	tv;
752#endif
753	udphdr_t	*u;
754	int	nfd, i;
755
756
757	ip->ip_hl = sizeof(*ip) >> 2;
758	ip->ip_v = IPVERSION;
759	ip->ip_tos = 0;
760	ip->ip_off = 0;
761	ip->ip_ttl = 60;
762	ip->ip_p = IPPROTO_UDP;
763	ip->ip_sum = 0;
764	u = (udphdr_t *)((char *)ip + (ip->ip_hl << 2));
765	u->uh_sport = htons(1);
766	u->uh_dport = htons(1);
767	u->uh_ulen = htons(sizeof(*u) + 4);
768	nfd = initdevice(dev, u->uh_sport, 1);
769
770	if (!ptest || (ptest == 1)) {
771		/*
772		 * Test 1. ulen > packet
773		 */
774		u->uh_ulen = htons(sizeof(*u) + 4);
775		ip->ip_len = (ip->ip_hl << 2) + ntohs(u->uh_ulen);
776		printf("4.1 UDP uh_ulen > packet size - short packets\n");
777		for (i = ntohs(u->uh_ulen) * 2; i > sizeof(*u) + 4; i--) {
778			u->uh_ulen = htons(i);
779			(void) send_udp(nfd, 1500, ip, gwip);
780			printf("%d\r", i);
781			fflush(stdout);
782			PAUSE();
783		}
784		putchar('\n');
785	}
786
787	if (!ptest || (ptest == 2)) {
788		/*
789		 * Test 2. ulen < packet
790		 */
791		u->uh_ulen = htons(sizeof(*u) + 4);
792		ip->ip_len = (ip->ip_hl << 2) + ntohs(u->uh_ulen);
793		printf("4.2 UDP uh_ulen < packet size - short packets\n");
794		for (i = ntohs(u->uh_ulen) * 2; i > sizeof(*u) + 4; i--) {
795			ip->ip_len = i;
796			(void) send_udp(nfd, 1500, ip, gwip);
797			printf("%d\r", i);
798			fflush(stdout);
799			PAUSE();
800		}
801		putchar('\n');
802	}
803
804	if (!ptest || (ptest == 3)) {
805		/*
806		 * Test 3: sport = 0, sport = 1, sport = 32767
807		 *         sport = 32768, sport = 65535
808		 */
809		u->uh_ulen = sizeof(*u) + 4;
810		ip->ip_len = (ip->ip_hl << 2) + ntohs(u->uh_ulen);
811		printf("4.3.1 UDP sport = 0\n");
812		u->uh_sport = 0;
813		(void) send_udp(nfd, 1500, ip, gwip);
814		printf("0\n");
815		fflush(stdout);
816		PAUSE();
817		printf("4.3.2 UDP sport = 1\n");
818		u->uh_sport = htons(1);
819		(void) send_udp(nfd, 1500, ip, gwip);
820		printf("1\n");
821		fflush(stdout);
822		PAUSE();
823		printf("4.3.3 UDP sport = 32767\n");
824		u->uh_sport = htons(32767);
825		(void) send_udp(nfd, 1500, ip, gwip);
826		printf("32767\n");
827		fflush(stdout);
828		PAUSE();
829		printf("4.3.4 UDP sport = 32768\n");
830		u->uh_sport = htons(32768);
831		(void) send_udp(nfd, 1500, ip, gwip);
832		printf("32768\n");
833		putchar('\n');
834		fflush(stdout);
835		PAUSE();
836		printf("4.3.5 UDP sport = 65535\n");
837		u->uh_sport = htons(65535);
838		(void) send_udp(nfd, 1500, ip, gwip);
839		printf("65535\n");
840		fflush(stdout);
841		PAUSE();
842	}
843
844	if (!ptest || (ptest == 4)) {
845		/*
846		 * Test 4: dport = 0, dport = 1, dport = 32767
847		 *         dport = 32768, dport = 65535
848		 */
849		u->uh_ulen = ntohs(sizeof(*u) + 4);
850		u->uh_sport = htons(1);
851		ip->ip_len = (ip->ip_hl << 2) + ntohs(u->uh_ulen);
852		printf("4.4.1 UDP dport = 0\n");
853		u->uh_dport = 0;
854		(void) send_udp(nfd, 1500, ip, gwip);
855		printf("0\n");
856		fflush(stdout);
857		PAUSE();
858		printf("4.4.2 UDP dport = 1\n");
859		u->uh_dport = htons(1);
860		(void) send_udp(nfd, 1500, ip, gwip);
861		printf("1\n");
862		fflush(stdout);
863		PAUSE();
864		printf("4.4.3 UDP dport = 32767\n");
865		u->uh_dport = htons(32767);
866		(void) send_udp(nfd, 1500, ip, gwip);
867		printf("32767\n");
868		fflush(stdout);
869		PAUSE();
870		printf("4.4.4 UDP dport = 32768\n");
871		u->uh_dport = htons(32768);
872		(void) send_udp(nfd, 1500, ip, gwip);
873		printf("32768\n");
874		fflush(stdout);
875		PAUSE();
876		printf("4.4.5 UDP dport = 65535\n");
877		u->uh_dport = htons(65535);
878		(void) send_udp(nfd, 1500, ip, gwip);
879		printf("65535\n");
880		fflush(stdout);
881		PAUSE();
882	}
883
884	if (!ptest || (ptest == 5)) {
885		/*
886		 * Test 5: sizeof(ip_t) <= MTU <= sizeof(udphdr_t) +
887		 * sizeof(ip_t)
888		 */
889		printf("4.5 UDP 20 <= MTU <= 32\n");
890		for (i = sizeof(*ip); i <= ntohs(u->uh_ulen); i++) {
891			(void) send_udp(nfd, i, ip, gwip);
892			printf("%d\r", i);
893			fflush(stdout);
894			PAUSE();
895		}
896		putchar('\n');
897	}
898}
899
900
901/* Perform test 5 (TCP) */
902
903void	ip_test5(dev, mtu, ip, gwip, ptest)
904char	*dev;
905int	mtu;
906ip_t	*ip;
907struct	in_addr	gwip;
908int	ptest;
909{
910#ifdef	USE_NANOSLEEP
911	struct	timespec ts;
912#else
913	struct	timeval	tv;
914#endif
915	tcphdr_t *t;
916	int	nfd, i;
917
918	t = (tcphdr_t *)((char *)ip + (ip->ip_hl << 2));
919#ifndef	linux
920	t->th_x2 = 0;
921#endif
922	t->th_off = 0;
923	t->th_sport = htons(1);
924	t->th_dport = htons(1);
925	t->th_win = htons(4096);
926	t->th_urp = 0;
927	t->th_sum = 0;
928	t->th_seq = htonl(1);
929	t->th_ack = 0;
930	ip->ip_len = sizeof(ip_t) + sizeof(tcphdr_t);
931	nfd = initdevice(dev, t->th_sport, 1);
932
933	if (!ptest || (ptest == 1)) {
934		/*
935		 * Test 1: flags variations, 0 - 3f
936		 */
937		t->th_off = sizeof(*t) >> 2;
938		printf("5.1 Test TCP flag combinations\n");
939		for (i = 0; i <= (TH_URG|TH_ACK|TH_PUSH|TH_RST|TH_SYN|TH_FIN);
940		     i++) {
941			t->th_flags = i;
942			(void) send_tcp(nfd, mtu, ip, gwip);
943			printf("%d\r", i);
944			fflush(stdout);
945			PAUSE();
946		}
947		putchar('\n');
948	}
949
950	if (!ptest || (ptest == 2)) {
951		t->th_flags = TH_SYN;
952		/*
953		 * Test 2: seq = 0, seq = 1, seq = 0x7fffffff, seq=0x80000000,
954		 *         seq = 0xa000000, seq = 0xffffffff
955		 */
956		printf("5.2.1 TCP seq = 0\n");
957		t->th_seq = htonl(0);
958		(void) send_tcp(nfd, mtu, ip, gwip);
959		fflush(stdout);
960		PAUSE();
961
962		printf("5.2.2 TCP seq = 1\n");
963		t->th_seq = htonl(1);
964		(void) send_tcp(nfd, mtu, ip, gwip);
965		fflush(stdout);
966		PAUSE();
967
968		printf("5.2.3 TCP seq = 0x7fffffff\n");
969		t->th_seq = htonl(0x7fffffff);
970		(void) send_tcp(nfd, mtu, ip, gwip);
971		fflush(stdout);
972		PAUSE();
973
974		printf("5.2.4 TCP seq = 0x80000000\n");
975		t->th_seq = htonl(0x80000000);
976		(void) send_tcp(nfd, mtu, ip, gwip);
977		fflush(stdout);
978		PAUSE();
979
980		printf("5.2.5 TCP seq = 0xc0000000\n");
981		t->th_seq = htonl(0xc0000000);
982		(void) send_tcp(nfd, mtu, ip, gwip);
983		fflush(stdout);
984		PAUSE();
985
986		printf("5.2.6 TCP seq = 0xffffffff\n");
987		t->th_seq = htonl(0xffffffff);
988		(void) send_tcp(nfd, mtu, ip, gwip);
989		fflush(stdout);
990		PAUSE();
991	}
992
993	if (!ptest || (ptest == 3)) {
994		t->th_flags = TH_ACK;
995		/*
996		 * Test 3: ack = 0, ack = 1, ack = 0x7fffffff, ack = 0x8000000
997		 *         ack = 0xa000000, ack = 0xffffffff
998		 */
999		printf("5.3.1 TCP ack = 0\n");
1000		t->th_ack = 0;
1001		(void) send_tcp(nfd, mtu, ip, gwip);
1002		fflush(stdout);
1003		PAUSE();
1004
1005		printf("5.3.2 TCP ack = 1\n");
1006		t->th_ack = htonl(1);
1007		(void) send_tcp(nfd, mtu, ip, gwip);
1008		fflush(stdout);
1009		PAUSE();
1010
1011		printf("5.3.3 TCP ack = 0x7fffffff\n");
1012		t->th_ack = htonl(0x7fffffff);
1013		(void) send_tcp(nfd, mtu, ip, gwip);
1014		fflush(stdout);
1015		PAUSE();
1016
1017		printf("5.3.4 TCP ack = 0x80000000\n");
1018		t->th_ack = htonl(0x80000000);
1019		(void) send_tcp(nfd, mtu, ip, gwip);
1020		fflush(stdout);
1021		PAUSE();
1022
1023		printf("5.3.5 TCP ack = 0xc0000000\n");
1024		t->th_ack = htonl(0xc0000000);
1025		(void) send_tcp(nfd, mtu, ip, gwip);
1026		fflush(stdout);
1027		PAUSE();
1028
1029		printf("5.3.6 TCP ack = 0xffffffff\n");
1030		t->th_ack = htonl(0xffffffff);
1031		(void) send_tcp(nfd, mtu, ip, gwip);
1032		fflush(stdout);
1033		PAUSE();
1034	}
1035
1036	if (!ptest || (ptest == 4)) {
1037		t->th_flags = TH_SYN;
1038		/*
1039		 * Test 4: win = 0, win = 32768, win = 65535
1040		 */
1041		printf("5.4.1 TCP win = 0\n");
1042		t->th_seq = htonl(0);
1043		(void) send_tcp(nfd, mtu, ip, gwip);
1044		fflush(stdout);
1045		PAUSE();
1046
1047		printf("5.4.2 TCP win = 32768\n");
1048		t->th_seq = htonl(0x7fff);
1049		(void) send_tcp(nfd, mtu, ip, gwip);
1050		fflush(stdout);
1051		PAUSE();
1052
1053		printf("5.4.3 TCP win = 65535\n");
1054		t->th_win = htons(0xffff);
1055		(void) send_tcp(nfd, mtu, ip, gwip);
1056		fflush(stdout);
1057		PAUSE();
1058	}
1059
1060#if !defined(linux) && !defined(__SVR4) && !defined(__svr4__) && \
1061    !defined(__sgi)
1062	{
1063	struct tcpcb *tcbp, tcb;
1064	struct tcpiphdr ti;
1065	struct sockaddr_in sin;
1066	int fd, slen;
1067
1068	fd = -1;
1069	bzero((char *)&sin, sizeof(sin));
1070
1071	for (i = 1; i < 63; i++) {
1072		fd = socket(AF_INET, SOCK_STREAM, 0);
1073		bzero((char *)&sin, sizeof(sin));
1074		sin.sin_addr.s_addr = ip->ip_dst.s_addr;
1075		sin.sin_port = htons(i);
1076		sin.sin_family = AF_INET;
1077		if (!connect(fd, (struct sockaddr *)&sin, sizeof(sin)))
1078			break;
1079		close(fd);
1080	}
1081
1082	if (i == 63) {
1083		printf("Couldn't open a TCP socket between ports 1 and 63\n");
1084		printf("to host %s for test 5 and 6 - skipping.\n",
1085			inet_ntoa(ip->ip_dst));
1086		goto skip_five_and_six;
1087	}
1088
1089	bcopy((char *)ip, (char *)&ti, sizeof(*ip));
1090	t->th_dport = htons(i);
1091	slen = sizeof(sin);
1092	if (!getsockname(fd, (struct sockaddr *)&sin, &slen))
1093		t->th_sport = sin.sin_port;
1094	if (!(tcbp = find_tcp(fd, &ti))) {
1095		printf("Can't find PCB\n");
1096		goto skip_five_and_six;
1097	}
1098	KMCPY(&tcb, tcbp, sizeof(tcb));
1099	ti.ti_win = tcb.rcv_adv;
1100	ti.ti_seq = htonl(tcb.snd_nxt - 1);
1101	ti.ti_ack = tcb.rcv_nxt;
1102
1103	if (!ptest || (ptest == 5)) {
1104		/*
1105		 * Test 5: urp
1106		 */
1107		t->th_flags = TH_ACK|TH_URG;
1108		printf("5.5.1 TCP Urgent pointer, sport %hu dport %hu\n",
1109			ntohs(t->th_sport), ntohs(t->th_dport));
1110		t->th_urp = htons(1);
1111		(void) send_tcp(nfd, mtu, ip, gwip);
1112		PAUSE();
1113
1114		t->th_seq = htonl(tcb.snd_nxt);
1115		ip->ip_len = sizeof(ip_t) + sizeof(tcphdr_t) + 1;
1116		t->th_urp = htons(0x7fff);
1117		(void) send_tcp(nfd, mtu, ip, gwip);
1118		PAUSE();
1119		t->th_urp = htons(0x8000);
1120		(void) send_tcp(nfd, mtu, ip, gwip);
1121		PAUSE();
1122		t->th_urp = htons(0xffff);
1123		(void) send_tcp(nfd, mtu, ip, gwip);
1124		PAUSE();
1125		t->th_urp = 0;
1126		t->th_flags &= ~TH_URG;
1127		ip->ip_len = sizeof(ip_t) + sizeof(tcphdr_t);
1128	}
1129
1130	if (!ptest || (ptest == 6)) {
1131		/*
1132		 * Test 6: data offset, off = 0, off is inside, off is outside
1133		 */
1134		t->th_flags = TH_ACK;
1135		printf("5.6.1 TCP off = 1-15, len = 40\n");
1136		for (i = 1; i < 16; i++) {
1137			ti.ti_off = ntohs(i);
1138			(void) send_tcp(nfd, mtu, ip, gwip);
1139			printf("%d\r", i);
1140			fflush(stdout);
1141			PAUSE();
1142		}
1143		putchar('\n');
1144		ip->ip_len = sizeof(ip_t) + sizeof(tcphdr_t);
1145	}
1146
1147	(void) close(fd);
1148	}
1149skip_five_and_six:
1150#endif
1151	t->th_seq = htonl(1);
1152	t->th_ack = htonl(1);
1153	t->th_off = 0;
1154
1155	if (!ptest || (ptest == 7)) {
1156		t->th_flags = TH_SYN;
1157		/*
1158		 * Test 7: sport = 0, sport = 1, sport = 32767
1159		 *         sport = 32768, sport = 65535
1160		 */
1161		printf("5.7.1 TCP sport = 0\n");
1162		t->th_sport = 0;
1163		(void) send_tcp(nfd, mtu, ip, gwip);
1164		fflush(stdout);
1165		PAUSE();
1166
1167		printf("5.7.2 TCP sport = 1\n");
1168		t->th_sport = htons(1);
1169		(void) send_tcp(nfd, mtu, ip, gwip);
1170		fflush(stdout);
1171		PAUSE();
1172
1173		printf("5.7.3 TCP sport = 32767\n");
1174		t->th_sport = htons(32767);
1175		(void) send_tcp(nfd, mtu, ip, gwip);
1176		fflush(stdout);
1177		PAUSE();
1178
1179		printf("5.7.4 TCP sport = 32768\n");
1180		t->th_sport = htons(32768);
1181		(void) send_tcp(nfd, mtu, ip, gwip);
1182		fflush(stdout);
1183		PAUSE();
1184
1185		printf("5.7.5 TCP sport = 65535\n");
1186		t->th_sport = htons(65535);
1187		(void) send_tcp(nfd, mtu, ip, gwip);
1188		fflush(stdout);
1189		PAUSE();
1190	}
1191
1192	if (!ptest || (ptest == 8)) {
1193		t->th_sport = htons(1);
1194		t->th_flags = TH_SYN;
1195		/*
1196		 * Test 8: dport = 0, dport = 1, dport = 32767
1197		 *         dport = 32768, dport = 65535
1198		 */
1199		printf("5.8.1 TCP dport = 0\n");
1200		t->th_dport = 0;
1201		(void) send_tcp(nfd, mtu, ip, gwip);
1202		fflush(stdout);
1203		PAUSE();
1204
1205		printf("5.8.2 TCP dport = 1\n");
1206		t->th_dport = htons(1);
1207		(void) send_tcp(nfd, mtu, ip, gwip);
1208		fflush(stdout);
1209		PAUSE();
1210
1211		printf("5.8.3 TCP dport = 32767\n");
1212		t->th_dport = htons(32767);
1213		(void) send_tcp(nfd, mtu, ip, gwip);
1214		fflush(stdout);
1215		PAUSE();
1216
1217		printf("5.8.4 TCP dport = 32768\n");
1218		t->th_dport = htons(32768);
1219		(void) send_tcp(nfd, mtu, ip, gwip);
1220		fflush(stdout);
1221		PAUSE();
1222
1223		printf("5.8.5 TCP dport = 65535\n");
1224		t->th_dport = htons(65535);
1225		(void) send_tcp(nfd, mtu, ip, gwip);
1226		fflush(stdout);
1227		PAUSE();
1228	}
1229
1230	/* LAND attack - self connect, so make src & dst ip/port the same */
1231	if (!ptest || (ptest == 9)) {
1232		printf("5.9 TCP LAND attack. sport = 25, dport = 25\n");
1233		/* chose SMTP port 25 */
1234		t->th_sport = htons(25);
1235		t->th_dport = htons(25);
1236		t->th_flags = TH_SYN;
1237		ip->ip_src = ip->ip_dst;
1238		(void) send_tcp(nfd, mtu, ip, gwip);
1239		fflush(stdout);
1240		PAUSE();
1241	}
1242
1243	/* TCP options header checking */
1244	/* 0 length options, etc */
1245}
1246
1247
1248/* Perform test 6 (exhaust mbuf test) */
1249
1250void	ip_test6(dev, mtu, ip, gwip, ptest)
1251char	*dev;
1252int	mtu;
1253ip_t	*ip;
1254struct	in_addr	gwip;
1255int	ptest;
1256{
1257#ifdef	USE_NANOSLEEP
1258	struct	timespec ts;
1259#else
1260	struct	timeval	tv;
1261#endif
1262	udphdr_t *u;
1263	int	nfd, i, j, k;
1264
1265	ip->ip_v = IPVERSION;
1266	ip->ip_tos = 0;
1267	ip->ip_off = 0;
1268	ip->ip_ttl = 60;
1269	ip->ip_p = IPPROTO_UDP;
1270	ip->ip_sum = 0;
1271	u = (udphdr_t *)(ip + 1);
1272	u->uh_sport = htons(1);
1273	u->uh_dport = htons(9);
1274	u->uh_sum = 0;
1275
1276	nfd = initdevice(dev, u->uh_sport, 1);
1277	u->uh_ulen = htons(7168);
1278
1279	printf("6. Exhaustive mbuf test.\n");
1280	printf("   Send 7k packet in 768 & 128 byte fragments, 128 times.\n");
1281	printf("   Total of around 8,900 packets\n");
1282	for (i = 0; i < 128; i++) {
1283		/*
1284		 * First send the entire packet in 768 byte chunks.
1285		 */
1286		ip->ip_len = sizeof(*ip) + 768 + sizeof(*u);
1287		ip->ip_hl = sizeof(*ip) >> 2;
1288		ip->ip_off = htons(IP_MF);
1289		(void) send_ip(nfd, 1500, ip, gwip, 1);
1290		printf("%d %d\r", i, 0);
1291		fflush(stdout);
1292		PAUSE();
1293		/*
1294		 * And again using 128 byte chunks.
1295		 */
1296		ip->ip_len = sizeof(*ip) + 128 + sizeof(*u);
1297		ip->ip_off = htons(IP_MF);
1298		(void) send_ip(nfd, 1500, ip, gwip, 1);
1299		printf("%d %d\r", i, 0);
1300		fflush(stdout);
1301		PAUSE();
1302
1303		for (j = 768; j < 3584; j += 768) {
1304			ip->ip_len = sizeof(*ip) + 768;
1305			ip->ip_off = htons(IP_MF|((j>>3) & 0x1fff));
1306			(void) send_ip(nfd, 1500, ip, gwip, 1);
1307			printf("%d %d\r", i, j);
1308			fflush(stdout);
1309			PAUSE();
1310
1311			ip->ip_len = sizeof(*ip) + 128;
1312			for (k = j - 768; k < j; k += 128) {
1313				ip->ip_off = htons(IP_MF|((k>>3) & 0x1fff));
1314				(void) send_ip(nfd, 1500, ip, gwip, 1);
1315				printf("%d %d\r", i, k);
1316				fflush(stdout);
1317				PAUSE();
1318			}
1319		}
1320	}
1321	putchar('\n');
1322}
1323
1324
1325/* Perform test 7 (random packets) */
1326
1327static	u_long	tbuf[64];
1328
1329void	ip_test7(dev, mtu, ip, gwip, ptest)
1330char	*dev;
1331int	mtu;
1332ip_t	*ip;
1333struct	in_addr	gwip;
1334int	ptest;
1335{
1336#ifdef	USE_NANOSLEEP
1337	struct	timespec ts;
1338#else
1339	struct	timeval	tv;
1340#endif
1341	ip_t	*pip;
1342	int	nfd, i, j;
1343	u_char	*s;
1344
1345	nfd = initdevice(dev, 0, 1);
1346	pip = (ip_t *)tbuf;
1347
1348	srand(time(NULL) ^ (getpid() * getppid()));
1349
1350	printf("7. send 1024 random IP packets.\n");
1351
1352	for (i = 0; i < 512; i++) {
1353		for (s = (u_char *)pip, j = 0; j < sizeof(tbuf); j++, s++)
1354			*s = (rand() >> 13) & 0xff;
1355		pip->ip_v = IPVERSION;
1356		bcopy((char *)&ip->ip_dst, (char *)&pip->ip_dst,
1357		      sizeof(struct in_addr));
1358		pip->ip_sum = 0;
1359		pip->ip_len &= 0xff;
1360		(void) send_ip(nfd, mtu, pip, gwip, 0);
1361		printf("%d\r", i);
1362		fflush(stdout);
1363		PAUSE();
1364	}
1365	putchar('\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		pip->ip_v = IPVERSION;
1371		pip->ip_off &= htons(0xc000);
1372		bcopy((char *)&ip->ip_dst, (char *)&pip->ip_dst,
1373		      sizeof(struct in_addr));
1374		pip->ip_sum = 0;
1375		pip->ip_len &= 0xff;
1376		(void) send_ip(nfd, mtu, pip, gwip, 0);
1377		printf("%d\r", i);
1378		fflush(stdout);
1379		PAUSE();
1380	}
1381	putchar('\n');
1382}
1383