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 t->th_x2 = 0;
907 TCP_OFF_A(t, 0);
908 t->th_sport = htons(1);
909 t->th_dport = htons(1);
910 t->th_win = htons(4096);
911 t->th_urp = 0;
912 t->th_sum = 0;
913 t->th_seq = htonl(1);
914 t->th_ack = 0;
915 ip->ip_len = sizeof(ip_t) + sizeof(tcphdr_t);
916
917 nfd = initdevice(dev, 1);
918 if (nfd == -1)
919 return;
920
921 if (!ptest || (ptest == 1)) {
922 /*
923 * Test 1: flags variations, 0 - 3f
924 */
925 TCP_OFF_A(t, sizeof(*t) >> 2);
926 printf("5.1 Test TCP flag combinations\n");
927 for (i = 0; i <= (TH_URG|TH_ACK|TH_PUSH|TH_RST|TH_SYN|TH_FIN);
928 i++) {
929 t->th_flags = i;
930 (void) send_tcp(nfd, mtu, ip, gwip);
931 printf("%d\r", i);
932 fflush(stdout);
933 PAUSE();
934 }
935 putchar('\n');
936 }
937
938 if (!ptest || (ptest == 2)) {
939 t->th_flags = TH_SYN;
940 /*
941 * Test 2: seq = 0, seq = 1, seq = 0x7fffffff, seq=0x80000000,
942 * seq = 0xa000000, seq = 0xffffffff
943 */
944 printf("5.2.1 TCP seq = 0\n");
945 t->th_seq = htonl(0);
946 (void) send_tcp(nfd, mtu, ip, gwip);
947 fflush(stdout);
948 PAUSE();
949
950 printf("5.2.2 TCP seq = 1\n");
951 t->th_seq = htonl(1);
952 (void) send_tcp(nfd, mtu, ip, gwip);
953 fflush(stdout);
954 PAUSE();
955
956 printf("5.2.3 TCP seq = 0x7fffffff\n");
957 t->th_seq = htonl(0x7fffffff);
958 (void) send_tcp(nfd, mtu, ip, gwip);
959 fflush(stdout);
960 PAUSE();
961
962 printf("5.2.4 TCP seq = 0x80000000\n");
963 t->th_seq = htonl(0x80000000);
964 (void) send_tcp(nfd, mtu, ip, gwip);
965 fflush(stdout);
966 PAUSE();
967
968 printf("5.2.5 TCP seq = 0xc0000000\n");
969 t->th_seq = htonl(0xc0000000);
970 (void) send_tcp(nfd, mtu, ip, gwip);
971 fflush(stdout);
972 PAUSE();
973
974 printf("5.2.6 TCP seq = 0xffffffff\n");
975 t->th_seq = htonl(0xffffffff);
976 (void) send_tcp(nfd, mtu, ip, gwip);
977 fflush(stdout);
978 PAUSE();
979 }
980
981 if (!ptest || (ptest == 3)) {
982 t->th_flags = TH_ACK;
983 /*
984 * Test 3: ack = 0, ack = 1, ack = 0x7fffffff, ack = 0x8000000
985 * ack = 0xa000000, ack = 0xffffffff
986 */
987 printf("5.3.1 TCP ack = 0\n");
988 t->th_ack = 0;
989 (void) send_tcp(nfd, mtu, ip, gwip);
990 fflush(stdout);
991 PAUSE();
992
993 printf("5.3.2 TCP ack = 1\n");
994 t->th_ack = htonl(1);
995 (void) send_tcp(nfd, mtu, ip, gwip);
996 fflush(stdout);
997 PAUSE();
998
999 printf("5.3.3 TCP ack = 0x7fffffff\n");
1000 t->th_ack = htonl(0x7fffffff);
1001 (void) send_tcp(nfd, mtu, ip, gwip);
1002 fflush(stdout);
1003 PAUSE();
1004
1005 printf("5.3.4 TCP ack = 0x80000000\n");
1006 t->th_ack = htonl(0x80000000);
1007 (void) send_tcp(nfd, mtu, ip, gwip);
1008 fflush(stdout);
1009 PAUSE();
1010
1011 printf("5.3.5 TCP ack = 0xc0000000\n");
1012 t->th_ack = htonl(0xc0000000);
1013 (void) send_tcp(nfd, mtu, ip, gwip);
1014 fflush(stdout);
1015 PAUSE();
1016
1017 printf("5.3.6 TCP ack = 0xffffffff\n");
1018 t->th_ack = htonl(0xffffffff);
1019 (void) send_tcp(nfd, mtu, ip, gwip);
1020 fflush(stdout);
1021 PAUSE();
1022 }
1023
1024 if (!ptest || (ptest == 4)) {
1025 t->th_flags = TH_SYN;
1026 /*
1027 * Test 4: win = 0, win = 32768, win = 65535
1028 */
1029 printf("5.4.1 TCP win = 0\n");
1030 t->th_seq = htonl(0);
1031 (void) send_tcp(nfd, mtu, ip, gwip);
1032 fflush(stdout);
1033 PAUSE();
1034
1035 printf("5.4.2 TCP win = 32768\n");
1036 t->th_seq = htonl(0x7fff);
1037 (void) send_tcp(nfd, mtu, ip, gwip);
1038 fflush(stdout);
1039 PAUSE();
1040
1041 printf("5.4.3 TCP win = 65535\n");
1042 t->th_win = htons(0xffff);
1043 (void) send_tcp(nfd, mtu, ip, gwip);
1044 fflush(stdout);
1045 PAUSE();
1046 }
1047
1048 #if !defined(linux) && !defined(__SVR4) && !defined(__svr4__) && \
1049 !defined(__sgi) && !defined(__hpux) && !defined(__osf__)
1050 {
1051 struct tcpcb *tcbp, tcb;
1052 struct tcpiphdr ti;
1053 struct sockaddr_in sin;
1054 int fd;
1055 socklen_t slen;
1056
1057 bzero((char *)&sin, sizeof(sin));
1058
1059 for (i = 1; i < 63; i++) {
1060 fd = socket(AF_INET, SOCK_STREAM, 0);
1061 bzero((char *)&sin, sizeof(sin));
1062 sin.sin_addr.s_addr = ip->ip_dst.s_addr;
1063 sin.sin_port = htons(i);
1064 sin.sin_family = AF_INET;
1065 if (!connect(fd, (struct sockaddr *)&sin, sizeof(sin)))
1066 break;
1067 close(fd);
1068 }
1069
1070 if (i == 63) {
1071 printf("Couldn't open a TCP socket between ports 1 and 63\n");
1072 printf("to host %s for test 5 and 6 - skipping.\n",
1073 inet_ntoa(ip->ip_dst));
1074 goto skip_five_and_six;
1075 }
1076
1077 bcopy((char *)ip, (char *)&ti, sizeof(*ip));
1078 t->th_dport = htons(i);
1079 slen = sizeof(sin);
1080 if (!getsockname(fd, (struct sockaddr *)&sin, &slen))
1081 t->th_sport = sin.sin_port;
1082 if (!(tcbp = find_tcp(fd, &ti))) {
1083 printf("Can't find PCB\n");
1084 goto skip_five_and_six;
1085 }
1086 KMCPY(&tcb, tcbp, sizeof(tcb));
1087 ti.ti_win = tcb.rcv_adv;
1088 ti.ti_seq = htonl(tcb.snd_nxt - 1);
1089 ti.ti_ack = tcb.rcv_nxt;
1090
1091 if (!ptest || (ptest == 5)) {
1092 /*
1093 * Test 5: urp
1094 */
1095 t->th_flags = TH_ACK|TH_URG;
1096 printf("5.5.1 TCP Urgent pointer, sport %hu dport %hu\n",
1097 ntohs(t->th_sport), ntohs(t->th_dport));
1098 t->th_urp = htons(1);
1099 (void) send_tcp(nfd, mtu, ip, gwip);
1100 PAUSE();
1101
1102 t->th_seq = htonl(tcb.snd_nxt);
1103 ip->ip_len = sizeof(ip_t) + sizeof(tcphdr_t) + 1;
1104 t->th_urp = htons(0x7fff);
1105 (void) send_tcp(nfd, mtu, ip, gwip);
1106 PAUSE();
1107 t->th_urp = htons(0x8000);
1108 (void) send_tcp(nfd, mtu, ip, gwip);
1109 PAUSE();
1110 t->th_urp = htons(0xffff);
1111 (void) send_tcp(nfd, mtu, ip, gwip);
1112 PAUSE();
1113 t->th_urp = 0;
1114 t->th_flags &= ~TH_URG;
1115 ip->ip_len = sizeof(ip_t) + sizeof(tcphdr_t);
1116 }
1117
1118 if (!ptest || (ptest == 6)) {
1119 /*
1120 * Test 6: data offset, off = 0, off is inside, off is outside
1121 */
1122 t->th_flags = TH_ACK;
1123 printf("5.6.1 TCP off = 1-15, len = 40\n");
1124 for (i = 1; i < 16; i++) {
1125 TCP_OFF_A(t, ntohs(i));
1126 (void) send_tcp(nfd, mtu, ip, gwip);
1127 printf("%d\r", i);
1128 fflush(stdout);
1129 PAUSE();
1130 }
1131 putchar('\n');
1132 ip->ip_len = sizeof(ip_t) + sizeof(tcphdr_t);
1133 }
1134
1135 (void) close(fd);
1136 }
1137 skip_five_and_six:
1138 #endif
1139 t->th_seq = htonl(1);
1140 t->th_ack = htonl(1);
1141 TCP_OFF_A(t, 0);
1142
1143 if (!ptest || (ptest == 7)) {
1144 t->th_flags = TH_SYN;
1145 /*
1146 * Test 7: sport = 0, sport = 1, sport = 32767
1147 * sport = 32768, sport = 65535
1148 */
1149 printf("5.7.1 TCP sport = 0\n");
1150 t->th_sport = 0;
1151 (void) send_tcp(nfd, mtu, ip, gwip);
1152 fflush(stdout);
1153 PAUSE();
1154
1155 printf("5.7.2 TCP sport = 1\n");
1156 t->th_sport = htons(1);
1157 (void) send_tcp(nfd, mtu, ip, gwip);
1158 fflush(stdout);
1159 PAUSE();
1160
1161 printf("5.7.3 TCP sport = 32767\n");
1162 t->th_sport = htons(32767);
1163 (void) send_tcp(nfd, mtu, ip, gwip);
1164 fflush(stdout);
1165 PAUSE();
1166
1167 printf("5.7.4 TCP sport = 32768\n");
1168 t->th_sport = htons(32768);
1169 (void) send_tcp(nfd, mtu, ip, gwip);
1170 fflush(stdout);
1171 PAUSE();
1172
1173 printf("5.7.5 TCP sport = 65535\n");
1174 t->th_sport = htons(65535);
1175 (void) send_tcp(nfd, mtu, ip, gwip);
1176 fflush(stdout);
1177 PAUSE();
1178 }
1179
1180 if (!ptest || (ptest == 8)) {
1181 t->th_sport = htons(1);
1182 t->th_flags = TH_SYN;
1183 /*
1184 * Test 8: dport = 0, dport = 1, dport = 32767
1185 * dport = 32768, dport = 65535
1186 */
1187 printf("5.8.1 TCP dport = 0\n");
1188 t->th_dport = 0;
1189 (void) send_tcp(nfd, mtu, ip, gwip);
1190 fflush(stdout);
1191 PAUSE();
1192
1193 printf("5.8.2 TCP dport = 1\n");
1194 t->th_dport = htons(1);
1195 (void) send_tcp(nfd, mtu, ip, gwip);
1196 fflush(stdout);
1197 PAUSE();
1198
1199 printf("5.8.3 TCP dport = 32767\n");
1200 t->th_dport = htons(32767);
1201 (void) send_tcp(nfd, mtu, ip, gwip);
1202 fflush(stdout);
1203 PAUSE();
1204
1205 printf("5.8.4 TCP dport = 32768\n");
1206 t->th_dport = htons(32768);
1207 (void) send_tcp(nfd, mtu, ip, gwip);
1208 fflush(stdout);
1209 PAUSE();
1210
1211 printf("5.8.5 TCP dport = 65535\n");
1212 t->th_dport = htons(65535);
1213 (void) send_tcp(nfd, mtu, ip, gwip);
1214 fflush(stdout);
1215 PAUSE();
1216 }
1217
1218 /* LAND attack - self connect, so make src & dst ip/port the same */
1219 if (!ptest || (ptest == 9)) {
1220 printf("5.9 TCP LAND attack. sport = 25, dport = 25\n");
1221 /* chose SMTP port 25 */
1222 t->th_sport = htons(25);
1223 t->th_dport = htons(25);
1224 t->th_flags = TH_SYN;
1225 ip->ip_src = ip->ip_dst;
1226 (void) send_tcp(nfd, mtu, ip, gwip);
1227 fflush(stdout);
1228 PAUSE();
1229 }
1230
1231 /* TCP options header checking */
1232 /* 0 length options, etc */
1233 }
1234
1235
1236 /* Perform test 6 (exhaust mbuf test) */
1237
1238 void
ip_test6(char * dev,int mtu,ip_t * ip,struct in_addr gwip,int ptest)1239 ip_test6(char *dev, int mtu, ip_t *ip, struct in_addr gwip, int ptest)
1240 {
1241 #ifdef USE_NANOSLEEP
1242 struct timespec ts;
1243 #else
1244 struct timeval tv;
1245 #endif
1246 udphdr_t *u;
1247 int nfd, i, j, k;
1248
1249 IP_V_A(ip, IPVERSION);
1250 ip->ip_tos = 0;
1251 ip->ip_off = 0;
1252 ip->ip_ttl = 60;
1253 ip->ip_p = IPPROTO_UDP;
1254 ip->ip_sum = 0;
1255 u = (udphdr_t *)(ip + 1);
1256 u->uh_sport = htons(1);
1257 u->uh_dport = htons(9);
1258 u->uh_sum = 0;
1259
1260 nfd = initdevice(dev, 1);
1261 if (nfd == -1)
1262 return;
1263
1264 u->uh_ulen = htons(7168);
1265
1266 printf("6. Exhaustive mbuf test.\n");
1267 printf(" Send 7k packet in 768 & 128 byte fragments, 128 times.\n");
1268 printf(" Total of around 8,900 packets\n");
1269 for (i = 0; i < 128; i++) {
1270 /*
1271 * First send the entire packet in 768 byte chunks.
1272 */
1273 ip->ip_len = sizeof(*ip) + 768 + sizeof(*u);
1274 IP_HL_A(ip, sizeof(*ip) >> 2);
1275 ip->ip_off = htons(IP_MF);
1276 (void) send_ip(nfd, 1500, ip, gwip, 1);
1277 printf("%d %d\r", i, 0);
1278 fflush(stdout);
1279 PAUSE();
1280 /*
1281 * And again using 128 byte chunks.
1282 */
1283 ip->ip_len = sizeof(*ip) + 128 + sizeof(*u);
1284 ip->ip_off = htons(IP_MF);
1285 (void) send_ip(nfd, 1500, ip, gwip, 1);
1286 printf("%d %d\r", i, 0);
1287 fflush(stdout);
1288 PAUSE();
1289
1290 for (j = 768; j < 3584; j += 768) {
1291 ip->ip_len = sizeof(*ip) + 768;
1292 ip->ip_off = htons(IP_MF|(j>>3));
1293 (void) send_ip(nfd, 1500, ip, gwip, 1);
1294 printf("%d %d\r", i, j);
1295 fflush(stdout);
1296 PAUSE();
1297
1298 ip->ip_len = sizeof(*ip) + 128;
1299 for (k = j - 768; k < j; k += 128) {
1300 ip->ip_off = htons(IP_MF|(k>>3));
1301 (void) send_ip(nfd, 1500, ip, gwip, 1);
1302 printf("%d %d\r", i, k);
1303 fflush(stdout);
1304 PAUSE();
1305 }
1306 }
1307 }
1308 putchar('\n');
1309 }
1310
1311
1312 /* Perform test 7 (random packets) */
1313
1314 static u_long tbuf[64];
1315
1316 void
ip_test7(char * dev,int mtu,ip_t * ip,struct in_addr gwip,int ptest)1317 ip_test7(char *dev, int mtu, ip_t *ip, struct in_addr gwip, int ptest)
1318 {
1319 ip_t *pip;
1320 #ifdef USE_NANOSLEEP
1321 struct timespec ts;
1322 #else
1323 struct timeval tv;
1324 #endif
1325 int nfd, i, j;
1326 u_char *s;
1327
1328 nfd = initdevice(dev, 1);
1329 if (nfd == -1)
1330 return;
1331
1332 pip = (ip_t *)tbuf;
1333
1334 srand(time(NULL) ^ (getpid() * getppid()));
1335
1336 printf("7. send 1024 random IP packets.\n");
1337
1338 for (i = 0; i < 512; i++) {
1339 for (s = (u_char *)pip, j = 0; j < sizeof(tbuf); j++, s++)
1340 *s = (rand() >> 13) & 0xff;
1341 IP_V_A(pip, IPVERSION);
1342 bcopy((char *)&ip->ip_dst, (char *)&pip->ip_dst,
1343 sizeof(struct in_addr));
1344 pip->ip_sum = 0;
1345 pip->ip_len &= 0xff;
1346 (void) send_ip(nfd, mtu, pip, gwip, 0);
1347 printf("%d\r", i);
1348 fflush(stdout);
1349 PAUSE();
1350 }
1351 putchar('\n');
1352
1353 for (i = 0; i < 512; i++) {
1354 for (s = (u_char *)pip, j = 0; j < sizeof(tbuf); j++, s++)
1355 *s = (rand() >> 13) & 0xff;
1356 IP_V_A(pip, IPVERSION);
1357 pip->ip_off &= htons(0xc000);
1358 bcopy((char *)&ip->ip_dst, (char *)&pip->ip_dst,
1359 sizeof(struct in_addr));
1360 pip->ip_sum = 0;
1361 pip->ip_len &= 0xff;
1362 (void) send_ip(nfd, mtu, pip, gwip, 0);
1363 printf("%d\r", i);
1364 fflush(stdout);
1365 PAUSE();
1366 }
1367 putchar('\n');
1368 }
1369