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