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