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