-
Notifications
You must be signed in to change notification settings - Fork 17
/
test-main.c
903 lines (758 loc) · 33.1 KB
/
test-main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
/**
Copyright (c) 2012 Aki Tuomi <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
**/
#include "responder.h"
#define ip_start test_ip_start // ensure non-clash
#include <assert.h>
#include <stdarg.h>
#include <net/if_arp.h>
#include <netinet/udp.h>
#define DEFAULT_SRC_MAC (u_char*)"\x46\x45\x44\x43\x42\x41"
#define DEFAULT_DST_MAC (u_char*)"\x36\x35\x34\x33\x32\x31"
#define DEFAULT_DST_IP (uint32_t*)"\xc0\xa8\x00\x02"
#define DEFAULT_SRC_IP (uint32_t*)"\xc0\xa8\x53\x47"
#define DEFAULT_IP_ADDR "192.168.0.2"
#define DEFAULT_IPSLA_PORT 50505
int tests_ok;
int tests_not_ok;
static int current_test = 0;
unsigned char test_result_buffer[ETH_FRAME_LEN+1];
ssize_t test_result_len;
static struct config_s config;
static ssize_t eth_o_vlan = ETH_O_VLAN;
static ssize_t ip_start = IP_START;
void test_log(const char * format, ...) {
char buf[4096];
size_t l;
time_t t;
struct tm tm;
va_list va;
va_start(va, format);
t = time(NULL);
localtime_r(&t, &tm);
l = strftime(buf, sizeof buf, "[%Y-%m-%d %H:%M:%S]: ", &tm);
vsnprintf(buf+l, sizeof(buf)-l, format, va);
printf("%s\n", buf);
}
void do_send(int sockfd, u_char *buf, size_t len) {
// makes sure we are not called wrong
assert(sockfd>0);
assert(buf != NULL);
assert(len>0);
assert(len<sizeof(test_result_buffer));
memcpy(test_result_buffer, buf, len);
test_result_len = len;
return;
}
void test_sanitize(void) {
memset(test_result_buffer,0,sizeof test_result_buffer);
test_result_len = 0;
}
ssize_t build_eth_header(const u_char *dmac, const u_char *smac, unsigned short vlan, unsigned short protocol, u_char *bytes) {
unsigned short val;
struct ether_header eh;
memcpy(eh.ether_dhost, dmac, ETH_ALEN);
memcpy(eh.ether_shost, smac, ETH_ALEN);
if (config.vlan) {
eh.ether_type = htons(ETH_P_8021Q);
val = htons(vlan);
memcpy(bytes+ETH_HLEN, &val, 2);
val = htons(protocol);
memcpy(bytes+ETH_HLEN+2, &val, 2);
} else
eh.ether_type = htons(protocol);
memcpy(bytes, &eh, ETH_HLEN);
return ETH_HLEN;
}
// ensure that the response comes from dest->src
int assert_eth_header(const u_char *bytes, unsigned short vlan, unsigned short protocol) {
struct ether_header eh;
memcpy(&eh, bytes, sizeof eh);
// check macs
if (memcmp(eh.ether_shost, DEFAULT_DST_MAC, ETH_ALEN)) {
test_log("Wrong source MAC in packet");
return 1;
}
if (memcmp(eh.ether_dhost, DEFAULT_SRC_MAC, ETH_ALEN)) {
test_log("Wrong destination MAC in packet");
return 1;
}
if (config.vlan) {
if (eh.ether_type != htons(ETH_P_8021Q) ||
*(uint16_t*)(bytes + ETH_HLEN) != htons(vlan) ||
*(uint16_t*)(bytes + ETH_HLEN + 2) != htons(protocol))
{
test_log("Wrong protocol number in packet");
return 1;
}
} else if (eh.ether_type != htons(protocol)) {
test_log("Wrong protocol number in packet");
return 1;
}
return 0;
}
void build_ip_header(uint32_t src_ip, uint32_t dst_ip, unsigned short data_len, unsigned short protocol, u_char *bytes) {
struct iphdr iph;
iph.version = 4;
iph.ihl = 5;
iph.tos = 0;
iph.tot_len = htons(data_len + sizeof(iph));
iph.id = 0x4545;
iph.frag_off = 0;
iph.ttl = 64;
iph.protocol = protocol;
iph.check = 0;
iph.saddr = src_ip;
iph.daddr = dst_ip;
// checksum
ip_checksum((const unsigned char*)&iph, sizeof(iph), &iph.check);
memcpy(bytes + ip_start, &iph, sizeof(iph));
}
void build_arp_pak(const u_char *dstmac, const u_char *srcmac, short arpop, uint32_t dstip, uint32_t srcip, u_char *bytes) {
struct arphdr ah;
build_eth_header(dstmac, srcmac, 42, ETH_P_ARP, bytes);
ah.ar_hrd = htons(ARPHRD_ETHER);
ah.ar_pro = htons(ETH_P_IP);
ah.ar_hln = ETH_ALEN;
ah.ar_pln = 4; // ip address size
ah.ar_op = htons(arpop);
memcpy(bytes + ip_start, &ah, sizeof ah);
memcpy(bytes + ip_start + sizeof(ah), srcmac, ETH_ALEN);
memcpy(bytes + ip_start + sizeof(ah) + ETH_ALEN, &srcip, 4);
memcpy(bytes + ip_start + sizeof(ah) + ETH_ALEN + 4, dstmac, ETH_ALEN);
memcpy(bytes + ip_start + sizeof(ah) + ETH_ALEN*2 + 4, &dstip, 4);
}
int assert_arp(u_char *bytes) {
struct arphdr ah;
if (assert_eth_header(bytes, 42, ETH_P_ARP)) return 1;
memcpy(&ah, bytes + ip_start, sizeof ah);
if (ah.ar_hrd != htons(ARPHRD_ETHER)) { test_log("invalid hwaddr in response"); return 1; }
if (ah.ar_pro != htons(ETH_P_IP)) { test_log("invalid protocol in response"); return 1; }
if (ah.ar_hln != ETH_ALEN) { test_log("invalid hwaddr length in response"); return 1; }
if (ah.ar_pln != 4) { test_log("invalid protocol length in response"); return 1; }
if (ah.ar_op != htons(ARPOP_REPLY)) { test_log("response is not reply"); return 1; }
// make sure it was intended for us.
if (memcmp(bytes + ip_start + sizeof(ah), DEFAULT_DST_MAC, ETH_ALEN)) { test_log("wrong source mac in reply"); return 1; }
if (memcmp(bytes + ip_start + sizeof(ah) + ETH_ALEN, DEFAULT_DST_IP, 4)) { test_log("wrong source ip in reply"); return 1; }
if (memcmp(bytes + ip_start + sizeof(ah) + ETH_ALEN + 4, DEFAULT_SRC_MAC, ETH_ALEN)) { test_log("wrong destination mac in reply"); return 1; }
if (memcmp(bytes + ip_start + sizeof(ah) + ETH_ALEN*2 + 4, DEFAULT_SRC_IP, 4)) { test_log("wrong destination ip in reply, %08x != %08x", *(uint32_t*)(bytes + ip_start + sizeof(ah) + ETH_ALEN*2 + 4), *DEFAULT_SRC_IP); return 1; }
return 0;
}
int assert_ip(u_char *bytes, uint32_t srcip, uint32_t dstip, unsigned short data_len, unsigned short proto) {
struct iphdr iph;
if (assert_eth_header(bytes, 42, ETH_P_IP)) return 1;
memcpy(&iph, bytes + ip_start, sizeof iph);
// checksum test
ip_checksum((const unsigned char*)&iph, sizeof(iph), &iph.check);
if (iph.check != 0) { test_log("ip checksum did not validate"); return 1; }
// check corrct protocol and such
if (iph.version != 4) { test_log("wrong ip version"); return 1; }
if (iph.ihl != 5) { test_log("ip header len != 5"); return 1; }
if (iph.tot_len != htons(data_len + sizeof(iph))) { test_log("total length wasn't as expected: wanted %u, got %u", data_len + sizeof(iph), ntohs(iph.tot_len)); return 1; }
if (iph.id != 0x4545) { test_log("IPID wrong"); return 1; }
if (iph.protocol != proto) { test_log("unexpected protocol: wanted %u, got %u", proto, iph.protocol); return 1; }
if (iph.saddr != srcip) { test_log("invalid source ip"); return 1; }
if (iph.daddr != dstip) { test_log("invalid destination ip"); return 1; }
return 0;
}
void do_pak_handler(const u_char *bytes, ssize_t len) {
struct pcap_pkthdr h;
struct pak_handler_s pak;
memset(&h, 0, sizeof(h));
h.len = h.caplen = len;
gettimeofday(&h.ts,NULL);
//bin2hex(bytes, h.caplen);
pak.config = &config;
pak.fd = 4;
pak_handler((u_char*)&pak, &h, bytes);
}
int test_valid_arp(void) {
u_char bytes[ETH_FRAME_LEN];
build_arp_pak(DEFAULT_DST_MAC, DEFAULT_SRC_MAC, ARPOP_REQUEST, *DEFAULT_DST_IP, *DEFAULT_SRC_IP, bytes);
do_pak_handler(bytes, 64);
if (test_result_len != 64) {
test_log("result is not 64 bytes long as expected, was %lu", test_result_len);
return 1;
}
return assert_arp(test_result_buffer);
}
int test_arp_not_for_me(void) {
u_char bytes[ETH_FRAME_LEN];
build_arp_pak(DEFAULT_SRC_MAC, DEFAULT_SRC_MAC, ARPOP_REQUEST, *DEFAULT_SRC_IP, *DEFAULT_SRC_IP, bytes);
do_pak_handler(bytes, 64);
if (test_result_len > 0 ) {
test_log("did not expect reply");
return 1;
}
return 0;
}
int test_invalid_arp(void) {
u_char bytes[ETH_FRAME_LEN];
build_arp_pak(DEFAULT_DST_MAC, DEFAULT_SRC_MAC, ARPOP_REQUEST, *DEFAULT_DST_IP, *DEFAULT_SRC_IP, bytes);
// do some slight corruption
bytes[ip_start] = 0x5;
do_pak_handler(bytes, 64);
if (test_result_len > 0 ) {
test_log("did not expect reply");
return 1;
}
return 0;
}
int test_broadcast_arp(void) {
u_char bytes[ETH_FRAME_LEN];
build_arp_pak((u_char*)"\xff\xff\xff\xff\xff\xff", DEFAULT_SRC_MAC, ARPOP_REQUEST, *DEFAULT_DST_IP, *DEFAULT_SRC_IP, bytes);
do_pak_handler(bytes, 64);
if (test_result_len != 64) {
test_log("result is not 64 bytes long as expected, was %lu", test_result_len);
return 1;
}
return assert_arp(test_result_buffer);
}
int test_checksum_ip(void) {
struct iphdr hdr;
hdr.version = 4;
hdr.ihl = 5;
hdr.tos = 0;
hdr.tot_len = htons(84);
hdr.id = 0x4545;
hdr.frag_off = 0;
hdr.ttl = 64;
hdr.protocol = 1;
hdr.check = 0xca60;
hdr.saddr = *DEFAULT_DST_IP;
hdr.daddr = *DEFAULT_SRC_IP;
ip_checksum((const unsigned char*)&hdr, sizeof(hdr), &hdr.check);
if (hdr.check != 0) {
test_log("Checksum wrong: (0 != %04x)", hdr.check);
}
return hdr.check;
}
int test_checksum_tcp(void) {
struct udphdr uh;
u_char data[64];
uh.source = htons(7);
uh.dest = htons(7);
uh.len = 64;
uh.check = 0xb423;
memcpy(data, &uh, sizeof uh);
memcpy(data+sizeof(uh), "\xbd\x3b\x78\xf8\xbc\x28\x41\x0f\xf7\xcd\x55\x91\xce\xa8\xe7\xac\xb3\xfe\x56\xd0\x6c\xa2\x1d\x41\xc9\x15\x8e\x74\xa0\x09\x4d\x2a\xe8\xd9\x76\xd9\x0c\x10\xb9\x65\x42\x11\xc9\x58\xbe\xce\x90\x89\x67\xaa\x56\xfa\xb7\x5e\xc0\xd0", 56); // just some random data to make things interesting
tcp4_checksum((u_char*)DEFAULT_SRC_IP, (u_char*)DEFAULT_DST_IP, 0x11, data, 64, &uh.check);
if (uh.check != 0) {
test_log("Checksum wrong: (0 != %04x)", uh.check);
}
return uh.check;
}
int test_icmp_ping(void) {
u_char bytes[ETH_FRAME_LEN];
struct icmphdr ih;
build_eth_header(DEFAULT_DST_MAC, DEFAULT_SRC_MAC, 42, ETH_P_IP, bytes);
// add IP header.
build_ip_header(*DEFAULT_SRC_IP, *DEFAULT_DST_IP, 64, 1, bytes);
ih.type = 8;
ih.code = 0;
ih.checksum = 0;
ih.un.echo.id = 0x4343;
ih.un.echo.sequence = 0x4242;
// copy into place
memcpy(bytes + ICMP_START, &ih, sizeof(ih));
// add some payload
memcpy(bytes + ICMP_DATA, "\xbd\x3b\x78\xf8\xbc\x28\x41\x0f\xf7\xcd\x55\x91\xce\xa8\xe7\xac\xb3\xfe\x56\xd0\x6c\xa2\x1d\x41\xc9\x15\x8e\x74\xa0\x09\x4d\x2a\xe8\xd9\x76\xd9\x0c\x10\xb9\x65\x42\x11\xc9\x58\xbe\xce\x90\x89\x67\xaa\x56\xfa\xb7\x5e\xc0\xd0", 56);
// checksum
ip_checksum(bytes + ICMP_START, sizeof(ih) + 56, (uint16_t*)(bytes + ICMP_START + 2));
// let's see what we get
do_pak_handler(bytes, 100 + 2*config.vlan);
if (test_result_len != 100 + 2*config.vlan) {
test_log("result is not 102 bytes long as expected, was %lu", test_result_len);
return 1;
}
if (assert_ip(test_result_buffer, *DEFAULT_DST_IP, *DEFAULT_SRC_IP, 64, 1)) return 1;
// check that all is good.
ip_checksum(test_result_buffer + ICMP_START, sizeof(ih) + 56, (uint16_t*)(test_result_buffer + ICMP_START + 2));
memcpy(&ih, test_result_buffer + ICMP_START, sizeof ih);
if (ih.checksum != 0) { test_log("ICMP packet checksum wrong"); return 1; }
if (ih.type != 0) { test_log("unexpected ICMP type in response"); return 1; }
if (ih.code != 0) { test_log("unexpected ICMP code in response"); return 1; }
if (ih.un.echo.id != 0x4343) { test_log("wrong ICMP echo id"); return 1; }
if (ih.un.echo.sequence != 0x4242) { test_log("wrong ICMP echo sequence"); return 1; }
// check payload
return memcmp(test_result_buffer + ICMP_DATA, "\xbd\x3b\x78\xf8\xbc\x28\x41\x0f\xf7\xcd\x55\x91\xce\xa8\xe7\xac\xb3\xfe\x56\xd0\x6c\xa2\x1d\x41\xc9\x15\x8e\x74\xa0\x09\x4d\x2a\xe8\xd9\x76\xd9\x0c\x10\xb9\x65\x42\x11\xc9\x58\xbe\xce\x90\x89\x67\xaa\x56\xfa\xb7\x5e\xc0\xd0", 56);
}
int test_df(void) {
u_char bytes[ETH_FRAME_LEN];
struct icmphdr ih;
build_eth_header(DEFAULT_DST_MAC, DEFAULT_SRC_MAC, 42, ETH_P_IP, bytes);
// add IP header.
build_ip_header(*DEFAULT_SRC_IP, *DEFAULT_DST_IP, 64, 1, bytes);
*(uint16_t*)(bytes + IP_O_FRAG_OFF) = 0x40; // DF
ih.type = 8;
ih.code = 0;
ih.checksum = 0;
ih.un.echo.id = 0x4343;
ih.un.echo.sequence = 0x4242;
// copy into place
memcpy(bytes + ICMP_START, &ih, sizeof(ih));
// add some payload
memcpy(bytes + ICMP_DATA, "\xbd\x3b\x78\xf8\xbc\x28\x41\x0f\xf7\xcd\x55\x91\xce\xa8\xe7\xac\xb3\xfe\x56\xd0\x6c\xa2\x1d\x41\xc9\x15\x8e\x74\xa0\x09\x4d\x2a\xe8\xd9\x76\xd9\x0c\x10\xb9\x65\x42\x11\xc9\x58\xbe\xce\x90\x89\x67\xaa\x56\xfa\xb7\x5e\xc0\xd0", 56);
// checksum
ip_checksum(bytes + ICMP_START, sizeof(ih) + 56, (uint16_t*)(bytes + ICMP_START + 2));
// let's see what we get
do_pak_handler(bytes, 100 + 2*config.vlan);
if (test_result_len != 100 + 2*config.vlan) {
test_log("result is not 102 bytes long as expected, was %lu", test_result_len);
return 1;
}
if (assert_ip(test_result_buffer, *DEFAULT_DST_IP, *DEFAULT_SRC_IP, 64, 1)) return 1;
// check that all is good.
ip_checksum(test_result_buffer + ICMP_START, sizeof(ih) + 56, (uint16_t*)(test_result_buffer + ICMP_START + 2));
memcpy(&ih, test_result_buffer + ICMP_START, sizeof ih);
if (ih.checksum != 0) { test_log("ICMP packet checksum wrong"); return 1; }
if (ih.type != 0) { test_log("unexpected ICMP type in response"); return 1; }
if (ih.code != 0) { test_log("unexpected ICMP code in response"); return 1; }
if (ih.un.echo.id != 0x4343) { test_log("wrong ICMP echo id"); return 1; }
if (ih.un.echo.sequence != 0x4242) { test_log("wrong ICMP echo sequence"); return 1; }
// check payload
return memcmp(test_result_buffer + ICMP_DATA, "\xbd\x3b\x78\xf8\xbc\x28\x41\x0f\xf7\xcd\x55\x91\xce\xa8\xe7\xac\xb3\xfe\x56\xd0\x6c\xa2\x1d\x41\xc9\x15\x8e\x74\xa0\x09\x4d\x2a\xe8\xd9\x76\xd9\x0c\x10\xb9\x65\x42\x11\xc9\x58\xbe\xce\x90\x89\x67\xaa\x56\xfa\xb7\x5e\xc0\xd0", 56);
}
int test_mf(void) {
u_char bytes[ETH_FRAME_LEN];
struct icmphdr ih;
build_eth_header(DEFAULT_DST_MAC, DEFAULT_SRC_MAC, 42, ETH_P_IP, bytes);
// add IP header.
build_ip_header(*DEFAULT_SRC_IP, *DEFAULT_DST_IP, 64, 1, bytes);
*(uint16_t*)(bytes + IP_O_FRAG_OFF) = 0x20; // MF
ih.type = 8;
ih.code = 0;
ih.checksum = 0;
ih.un.echo.id = 0x4343;
ih.un.echo.sequence = 0x4242;
// copy into place
memcpy(bytes + ICMP_START, &ih, sizeof(ih));
// add some payload
memcpy(bytes + ICMP_DATA, "\xbd\x3b\x78\xf8\xbc\x28\x41\x0f\xf7\xcd\x55\x91\xce\xa8\xe7\xac\xb3\xfe\x56\xd0\x6c\xa2\x1d\x41\xc9\x15\x8e\x74\xa0\x09\x4d\x2a\xe8\xd9\x76\xd9\x0c\x10\xb9\x65\x42\x11\xc9\x58\xbe\xce\x90\x89\x67\xaa\x56\xfa\xb7\x5e\xc0\xd0", 56);
// checksum
ip_checksum(bytes + ICMP_START, sizeof(ih) + 56, (uint16_t*)(bytes + ICMP_START + 2));
// let's see what we get
do_pak_handler(bytes, 100 + 2*config.vlan);
if (test_result_len) {
test_log("did not expect reply");
return 1;
}
return 0;
}
int test_junos_icmp_rpm(void) {
u_char bytes[ETH_FRAME_LEN],*ptr;
struct icmphdr ih;
struct timespec ts;
uint32_t tval;
build_eth_header(DEFAULT_DST_MAC, DEFAULT_SRC_MAC, 42, ETH_P_IP, bytes);
build_ip_header(*DEFAULT_SRC_IP, *DEFAULT_DST_IP, 64, 1, bytes);
ptr = bytes + ip_start + sizeof(struct ip);
ih.type = ICMP_TIMESTAMP;
ih.code = 0;
ih.checksum = 0;
ih.un.echo.id = 0x4343;
ih.un.echo.sequence = 0x4242;
memcpy(ptr, &ih, sizeof(ih));
ptr += sizeof(ih);
// toss in some stamps
clock_gettime(CLOCK_REALTIME, &ts);
tval = get_ts_utc(&ts);
memcpy(ptr, &tval, 4);
ptr += 4;
tval = 0;
memcpy(ptr, &tval, 4);
ptr += 4;
memcpy(ptr, &tval, 4);
// then we add some junos specific stuff
ptr += 20;
memcpy(ptr, "\x00\x01\x96\x10", 4);
ptr += 8;
// put a stamp here...
memcpy(ptr, "\x01\x02\x03\x04\x05\x06\x07\x08", 8);
// checksum
ip_checksum(bytes + ICMP_START, 64, (uint16_t*)(bytes + ICMP_START + 2));
// transmit
// emulate 0.1s delay
usleep(10000);
do_pak_handler(bytes, 102);
// did we get anything?
if (test_result_len != 102) {
test_log("result is not 102 bytes long as expected, was %lu", test_result_len);
return 1;
}
if (assert_ip(test_result_buffer, *DEFAULT_DST_IP, *DEFAULT_SRC_IP, 64, 1)) return 1;
ip_checksum(test_result_buffer + ip_start + sizeof(struct ip), sizeof(ih)+56, (uint16_t*)(test_result_buffer + ip_start + sizeof(struct ip) + 2));
memcpy(&ih, test_result_buffer + ip_start + sizeof(struct ip), sizeof ih);
if (ih.checksum != 0) { test_log("ICMP packet checksum wrong"); return 1; }
if (ih.type != ICMP_TIMESTAMPREPLY) { test_log("unexpected ICMP type in response"); return 1; }
if (ih.code != 0) { test_log("unexpected ICMP code in response"); return 1; }
if (ih.un.echo.id != 0x4343) { test_log("wrong ICMP echo id"); return 1; }
if (ih.un.echo.sequence != 0x4242) { test_log("wrong ICMP echo sequence"); return 1; }
// ensure that the originate timestamp <= recv timestamp <= transmit timestamp
if (*(uint32_t*)(test_result_buffer + ICMP_DATA) > *(uint32_t*)(test_result_buffer + ICMP_DATA + 0x4) ||
*(uint32_t*)(test_result_buffer + ICMP_DATA) > *(uint32_t*)(test_result_buffer + ICMP_DATA + 0x8) ||
*(uint32_t*)(test_result_buffer + ICMP_DATA + 0x4) > *(uint32_t*)(test_result_buffer + ICMP_DATA + 0x8)) {
test_log("originate <= receive <= transmit did not match: got %08x <= %08x <= %08x",
*(uint32_t*)(test_result_buffer + ICMP_DATA),
*(uint32_t*)(test_result_buffer + ICMP_DATA + 0x4),
*(uint32_t*)(test_result_buffer + ICMP_DATA + 0x8));
return 1;
}
// ensure that we have some stamp in junos reply
if (*(uint64_t*)(test_result_buffer + ICMP_DATA + 0x24) == 0) {
test_log("missing hardware timestamp in reply");
return 1;
}
return 0;
}
int test_icmp_timestamp(void) {
u_char bytes[ETH_FRAME_LEN],*ptr;
u_char fillval[] = "0123456789abcdef";
struct icmphdr ih;
struct timespec ts;
uint32_t tval;
build_eth_header(DEFAULT_DST_MAC, DEFAULT_SRC_MAC, 42, ETH_P_IP, bytes);
build_ip_header(*DEFAULT_SRC_IP, *DEFAULT_DST_IP, 64, 1, bytes);
ptr = bytes + ip_start + sizeof(struct ip);
// fill in the entire ICMP with 0123456789abcdef, then put in some stuff
for(tval=0;tval<4;tval++)
memcpy(ptr + tval*16, fillval, 16);
ih.type = ICMP_TIMESTAMP;
ih.code = 0;
ih.checksum = 0;
ih.un.echo.id = 0x4343;
ih.un.echo.sequence = 0x4242;
memcpy(ptr, &ih, sizeof(ih));
ptr += sizeof(ih);
// toss in some stamps
clock_gettime(CLOCK_REALTIME, &ts);
tval = get_ts_utc(&ts);
memcpy(ptr, &tval, 4);
ptr += 4;
tval = 0;
memcpy(ptr, &tval, 4);
ptr += 4;
memcpy(ptr, &tval, 4);
// checksum
ip_checksum(bytes + ICMP_START, 64, (uint16_t*)(bytes + ICMP_START + 2));
// transmit
// emulate 0.1s delay
usleep(10000);
do_pak_handler(bytes, 102);
// did we get anything?
if (test_result_len != 102) {
test_log("result is not 102 bytes long as expected, was %lu", test_result_len);
return 1;
}
if (assert_ip(test_result_buffer, *DEFAULT_DST_IP, *DEFAULT_SRC_IP, 64, 1)) return 1;
ip_checksum(test_result_buffer + ip_start + sizeof(struct ip), sizeof(ih)+56, (uint16_t*)(test_result_buffer + ip_start + sizeof(struct ip) + 2));
memcpy(&ih, test_result_buffer + ip_start + sizeof(struct ip), sizeof ih);
if (ih.checksum != 0) { test_log("ICMP packet checksum wrong"); return 1; }
if (ih.type != ICMP_TIMESTAMPREPLY) { test_log("unexpected ICMP type in response"); return 1; }
if (ih.code != 0) { test_log("unexpected ICMP code in response"); return 1; }
if (ih.un.echo.id != 0x4343) { test_log("wrong ICMP echo id"); return 1; }
if (ih.un.echo.sequence != 0x4242) { test_log("wrong ICMP echo sequence"); return 1; }
// ensure that the originate timestamp <= recv timestamp <= transmit timestamp
if (*(uint32_t*)(test_result_buffer + ICMP_DATA) > *(uint32_t*)(test_result_buffer + ICMP_DATA + 0x4) ||
*(uint32_t*)(test_result_buffer + ICMP_DATA) > *(uint32_t*)(test_result_buffer + ICMP_DATA + 0x8) ||
*(uint32_t*)(test_result_buffer + ICMP_DATA + 0x4) > *(uint32_t*)(test_result_buffer + ICMP_DATA + 0x8)) {
test_log("originate <= receive <= transmit did not match: got %08x <= %08x <= %08x",
*(uint32_t*)(test_result_buffer + ICMP_DATA),
*(uint32_t*)(test_result_buffer + ICMP_DATA + 0x4),
*(uint32_t*)(test_result_buffer + ICMP_DATA + 0x8));
return 1;
}
return 0;
}
int test_udp_cisco_init(void) {
u_char bytes[ETH_FRAME_LEN],*ptr;
struct udphdr uh;
struct timespec ts;
memset(bytes, 0, ETH_FRAME_LEN);
build_eth_header(DEFAULT_DST_MAC, DEFAULT_SRC_MAC, 42, ETH_P_IP, bytes);
build_ip_header(*DEFAULT_SRC_IP, *DEFAULT_DST_IP, 32, 17, bytes);
ptr = bytes + ip_start + sizeof(struct ip);
uh.source = htons(4242);
uh.dest = htons(1967);
// packet has 8 bytes of data and 24 bytes of payload
uh.len = htons(32);
uh.check = 0;
// put it in place
memcpy(ptr, &uh, sizeof(uh));
ptr += sizeof(uh);
// put in the actual data
*(ptr) = 0x01;
memcpy(ptr+0x10, DEFAULT_DST_IP, 4);
*((unsigned short*)(ptr+0x14)) = htons(50505); // actual testing port
// then calculate checksum
tcp4_checksum((u_char*)DEFAULT_SRC_IP, (u_char*)DEFAULT_DST_IP, 0x11, bytes+UDP_START, 32, (unsigned short*)(bytes+UDP_START+0x06));
// and that's it.
do_pak_handler(bytes, 66 + eth_o_vlan);
// did we get anything?
if (test_result_len != 66 + eth_o_vlan) {
test_log("result is not %u bytes long as expected, was %lu", 70, test_result_len);
return 1;
}
if (assert_ip(test_result_buffer, *DEFAULT_DST_IP, *DEFAULT_SRC_IP, 32, 17)) return 1;
// checksum check
tcp4_checksum((u_char*)DEFAULT_SRC_IP, (u_char*)DEFAULT_DST_IP, 0x11, test_result_buffer+UDP_START, 32, (unsigned short*)(test_result_buffer+UDP_START+0x06));
if (*((unsigned short*)(test_result_buffer+UDP_START+0x06)) != 0x0) {
test_log("invalid TCP checksum, ended up with %02x", (unsigned short*)(test_result_buffer+UDP_START+0x06));
return 1;
}
// check that something nice was sent.
if (*(test_result_buffer + UDP_DATA + 0x03) != 0x08 ||
memcmp(test_result_buffer + UDP_DATA + 0x04, "\0\0\0\0\0\0\0\0", 8)) {
test_log("invalid response - check your code");
return 1;
}
return 0;
}
int test_udp_cisco_echo(void) {
u_char bytes[ETH_FRAME_LEN],*ptr;
struct udphdr uh;
struct timespec ts;
memset(bytes, 0, ETH_FRAME_LEN);
build_eth_header(DEFAULT_DST_MAC, DEFAULT_SRC_MAC, 42, ETH_P_IP, bytes);
build_ip_header(*DEFAULT_SRC_IP, *DEFAULT_DST_IP, 40, 17, bytes);
ptr = bytes + ip_start + sizeof(struct ip);
uh.source = htons(4242);
uh.dest = htons(50505);
// packet has 8 bytes of data and 24 bytes of payload
uh.len = htons(24);
uh.check = 0;
// put it in place
memcpy(ptr, &uh, sizeof(uh));
ptr += sizeof(uh);
// fill with UDP echo specific data
*(uint16_t*)(ptr) = htons(1);
*(uint16_t*)(ptr + 0x2) = 0;
*(uint16_t*)(ptr + 0x4) = htons(24);
memcpy(ptr + 0x6, "ABABABABAB", 10);
// then calculate checksum
tcp4_checksum((u_char*)DEFAULT_SRC_IP, (u_char*)DEFAULT_DST_IP, 0x11, bytes+UDP_START, 40, (unsigned short*)(bytes+UDP_START+0x06));
// and that's it.
// emulate 0.1s delay
usleep(10000);
do_pak_handler(bytes, 58 + eth_o_vlan);
// did we get anything?
if (test_result_len != 58 + eth_o_vlan) {
test_log("result is not %u bytes long as expected, was %lu", 58+eth_o_vlan, test_result_len);
return 1;
}
if (assert_ip(test_result_buffer, *DEFAULT_DST_IP, *DEFAULT_SRC_IP, 40, 17)) return 1;
// checksum check
tcp4_checksum((u_char*)DEFAULT_SRC_IP, (u_char*)DEFAULT_DST_IP, 0x11, test_result_buffer+UDP_START, 16 + (UDP_DATA - UDP_START), (unsigned short*)(test_result_buffer+UDP_START+0x06));
if (*((unsigned short*)(test_result_buffer+UDP_START+0x06)) != 0x0) {
test_log("invalid TCP checksum, ended up with %02x", (unsigned short*)(test_result_buffer+UDP_START+0x06));
return 1;
}
// make sure it matches
ptr = test_result_buffer + UDP_DATA;
if (*(uint16_t*)(ptr) != htons(1)) {
test_log("invalid version number, expected 1, got %u", ntohs(*(uint16_t*)(ptr)));
return 1;
}
if (memcmp(ptr + 0x6, "ABABABABAB", 10)) {
test_log("invalid test pattern received");
return 1;
}
return 0;
}
int test_udp_cisco_jitter_type_2(void) {
u_char bytes[ETH_FRAME_LEN],*ptr;
struct udphdr uh;
struct timespec ts;
memset(bytes, 0, ETH_FRAME_LEN);
build_eth_header(DEFAULT_DST_MAC, DEFAULT_SRC_MAC, 42, ETH_P_IP, bytes);
build_ip_header(*DEFAULT_SRC_IP, *DEFAULT_DST_IP, 40, 17, bytes);
ptr = bytes + ip_start + sizeof(struct ip);
uh.source = htons(4242);
uh.dest = htons(50505);
// packet has 8 bytes of data and 32 bytes of payload
uh.len = htons(40);
uh.check = 0;
// put it in place
memcpy(ptr, &uh, sizeof(uh));
ptr += sizeof(uh);
clock_gettime(CLOCK_REALTIME, &ts);
// put in the actual data, milliseconds
*(uint16_t*)(ptr) = htons(2);
*(uint32_t*)(ptr + 0x4) = htonl(get_ts_utc(&ts));
*(uint16_t*)(ptr + 0xc) = 0x123;
// then calculate checksum
tcp4_checksum((u_char*)DEFAULT_SRC_IP, (u_char*)DEFAULT_DST_IP, 0x11, bytes+UDP_START, 40, (unsigned short*)(bytes+UDP_START+0x06));
// and that's it.
// emulate 0.1s delay
usleep(10000);
do_pak_handler(bytes, 74 + eth_o_vlan);
// did we get anything?
if (test_result_len != 74 + eth_o_vlan) {
test_log("result is not %u bytes long as expected, was %lu", 74+eth_o_vlan, test_result_len);
return 1;
}
if (assert_ip(test_result_buffer, *DEFAULT_DST_IP, *DEFAULT_SRC_IP, 40, 17)) return 1;
// checksum check
tcp4_checksum((u_char*)DEFAULT_SRC_IP, (u_char*)DEFAULT_DST_IP, 0x11, test_result_buffer+UDP_START, 40, (unsigned short*)(test_result_buffer+UDP_START+0x06));
if (*((unsigned short*)(test_result_buffer+UDP_START+0x06)) != 0x0) {
test_log("invalid TCP checksum, ended up with %02x", (unsigned short*)(test_result_buffer+UDP_START+0x06));
return 1;
}
// check that sequence number is correct
if (*(uint16_t*)(test_result_buffer + UDP_DATA + 0xc) != *(uint16_t*)(test_result_buffer + UDP_DATA + 0xe)) {
test_log("got invalid sequence number, expected %02x, got %02x", *(uint16_t*)(test_result_buffer + UDP_DATA + 0xc), *(uint16_t*)(test_result_buffer + UDP_DATA + 0xe));
return 1;
}
// check that timestamp is bigger
if (*(uint32_t*)(test_result_buffer + UDP_DATA + 0x4) > *(uint32_t*)(test_result_buffer + UDP_DATA + 0x8)) {
test_log("t1 timestamp was larger than t2 timestamp");
return 1;
}
return 0;
}
int test_udp_cisco_jitter_type_3(void) {
u_char bytes[ETH_FRAME_LEN],*ptr;
struct udphdr uh;
struct timespec ts;
uint32_t t2, t3;
memset(bytes, 0, ETH_FRAME_LEN);
build_eth_header(DEFAULT_DST_MAC, DEFAULT_SRC_MAC, 42, ETH_P_IP, bytes);
build_ip_header(*DEFAULT_SRC_IP, *DEFAULT_DST_IP, 40, 17, bytes);
ptr = bytes + ip_start + sizeof(struct ip);
uh.source = htons(4242);
uh.dest = htons(50505);
// packet has 8 bytes of data and 32 bytes of payload
uh.len = htons(40);
uh.check = 0;
// put it in place
memcpy(ptr, &uh, sizeof(uh));
ptr += sizeof(uh);
clock_gettime(CLOCK_REALTIME, &ts);
// put in the actual data, microseconds
*(uint16_t*)(ptr) = htons(3);
ts_to_ntp(&ts, &t2, &t3);
*(uint32_t*)(ptr+0x4) = t2;
*(uint32_t*)(ptr+0x8) = t3;
*(uint16_t*)(ptr+0x34) = 0x123;
// then calculate checksum
tcp4_checksum((u_char*)DEFAULT_SRC_IP, (u_char*)DEFAULT_DST_IP, 0x11, bytes+UDP_START, 40, (unsigned short*)(bytes+UDP_START+0x06));
// and that's it.
// emulate 0.1s delay
usleep(10000);
do_pak_handler(bytes, 74 + eth_o_vlan);
// did we get anything?
if (test_result_len != 74 + eth_o_vlan) {
test_log("result is not %u bytes long as expected, was %lu", 74 + eth_o_vlan, test_result_len);
return 1;
}
if (assert_ip(test_result_buffer, *DEFAULT_DST_IP, *DEFAULT_SRC_IP, 40, 17)) return 1;
// checksum check
tcp4_checksum((u_char*)DEFAULT_SRC_IP, (u_char*)DEFAULT_DST_IP, 0x11, test_result_buffer+UDP_START, 40, (unsigned short*)(test_result_buffer+UDP_START+0x06));
if (*((unsigned short*)(test_result_buffer+UDP_START+0x06)) != 0x0) {
test_log("invalid TCP checksum, ended up with %02x", (unsigned short*)(test_result_buffer+UDP_START+0x06));
return 1;
}
// check that sequence number is correct
if (*(uint16_t*)(test_result_buffer + UDP_DATA + 0x34) != *(uint16_t*)(test_result_buffer + UDP_DATA + 0x36)) {
test_log("got invalid sequence number, expected %02x, got %02x", *(uint16_t*)(test_result_buffer + UDP_DATA + 0x34), *(uint16_t*)(test_result_buffer + UDP_DATA + 0x36));
return 1;
}
// check that timestamp is bigger
if (ntohl(*(uint32_t*)(test_result_buffer + UDP_DATA + 0x4)) >= ntohl(*(uint32_t*)(test_result_buffer + UDP_DATA + 0xC)) &&
ntohl(*(uint32_t*)(test_result_buffer + UDP_DATA + 0x8)) >= ntohl(*(uint32_t*)(test_result_buffer + UDP_DATA + 0x10))) {
test_log("t1 timestamp was larger than t2 timestamp");
return 1;
}
return 0;
}
int test_udp_junos_rpm(void) {
test_log("not implemented");
return 0;
}
void run_test(int (*test_item)(void), const char *test_name) {
test_log("test #%04d: %s", ++current_test, test_name);
test_sanitize();
if (test_item() != 0) {
test_log("test result: FAILED");
bin2hex(test_result_buffer, test_result_len);
tests_not_ok++;
} else {
test_log("test result: PASS");
tests_ok++;
}
}
/**
* * main(int argc, char * const argv[])
* *
* * program entry point
* */
int main(int argc, char * const argv[]) {
int result = 0;
memcpy(config.mac, DEFAULT_DST_MAC, ETH_ALEN);
inet_pton(AF_INET, DEFAULT_IP_ADDR, &config.ip_addr);
config.debuglevel = 2;
config.cisco_port_low = config.cisco_port_high = DEFAULT_IPSLA_PORT;
config.do_ip4 = 1;
tests_ok = tests_not_ok = 0;
eth_o_vlan = 0;
ip_start = IP_START;
config.vlan = 0;
// run tests twice.
test_log("Testing without VLANs");
run_test(test_valid_arp, "replies to valid arp");
run_test(test_arp_not_for_me, "ignores arp not intended for us");
run_test(test_invalid_arp, "ignores invalid arp");
run_test(test_broadcast_arp, "replies to broadcast for our IP");
run_test(test_checksum_ip, "produces valid ip checksum");
run_test(test_checksum_tcp, "produces valid tcp checksum");
run_test(test_icmp_ping, "responds to ICMP echo request");
run_test(test_df, "responds to ICMP echo request with DF set");
run_test(test_mf, "drops fragmented IPv4");
run_test(test_junos_icmp_rpm, "responds to juniper ICMP RPM ping");
run_test(test_icmp_timestamp, "responds to ICMP timestamp without junos RPM");
run_test(test_udp_cisco_init, "responds to Cisco UDP IP SLA initialization");
run_test(test_udp_cisco_jitter_type_2, "responds to Cisco UDP IP SLA jitter measurement (milliseconds)");
run_test(test_udp_cisco_jitter_type_3, "responds to Cisco UDP IP SLA jitter measurement (microseconds)");
run_test(test_udp_cisco_echo, "responds to UDP echo");
run_test(test_udp_junos_rpm, "responds to juniper UDP RPM ping");
printf("OK: %d NOT OK: %d SUCCESS %0.02f%%\n", tests_ok, tests_not_ok, (double)tests_ok/(double)(tests_ok+tests_not_ok)*100.0);
result = (tests_not_ok>0);
tests_ok = tests_not_ok = 0;
eth_o_vlan = ETH_O_VLAN;
ip_start = IP_START + ETH_O_VLAN;
config.vlan = 1;
test_log("Testing with VLANs");
run_test(test_valid_arp, "replies to valid arp");
run_test(test_arp_not_for_me, "ignores arp not intended for us");
run_test(test_invalid_arp, "ignores invalid arp");
run_test(test_broadcast_arp, "replies to broadcast for our IP");
run_test(test_checksum_ip, "produces valid ip checksum");
run_test(test_checksum_tcp, "produces valid tcp checksum");
run_test(test_icmp_ping, "responds to ICMP echo request");
run_test(test_junos_icmp_rpm, "responds to juniper ICMP RPM ping");
run_test(test_icmp_timestamp, "responds to ICMP timestamp without junos RPM");
run_test(test_udp_cisco_init, "responds to Cisco UDP IP SLA initialization");
run_test(test_udp_cisco_jitter_type_2, "responds to Cisco UDP IP SLA jitter measurement (milliseconds)");
run_test(test_udp_cisco_jitter_type_3, "responds to Cisco UDP IP SLA jitter measurement (microseconds)");
run_test(test_udp_cisco_echo, "responds to UDP echo");
run_test(test_udp_junos_rpm, "responds to juniper UDP RPM ping");
printf("OK: %d NOT OK: %d SUCCESS %0.02f%%\n", tests_ok, tests_not_ok, (double)tests_ok/(double)(tests_ok+tests_not_ok)*100.0);
result = (tests_not_ok>0);
return result;
}