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