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