forked from wangyu-/udp2raw
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnetwork.cpp
2153 lines (1778 loc) · 52.9 KB
/
network.cpp
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
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* network.cpp
*
* Created on: Jul 29, 2017
* Author: wangyu
*/
#include "common.h"
#include "network.h"
#include "log.h"
#include "misc.h"
int raw_recv_fd=-1;
int raw_send_fd=-1;
u32_t link_level_header_len=0;//set it to 14 if SOCK_RAW is used in socket(PF_PACKET, SOCK_RAW, htons(ETH_P_IP));
int seq_mode=3;
int max_seq_mode=4;
int random_drop=0;
int filter_port=-1;
int disable_bpf_filter=0; //for test only,most time no need to disable this
//u32_t bind_address_uint32=0;
int lower_level=0;
int lower_level_manual=0;
int ifindex=-1;
char if_name[100]="";
char dev[100]="";
unsigned short g_ip_id_counter=0;
unsigned char dest_hw_addr[sizeof(sockaddr_ll::sll_addr)]=
{0xff,0xff,0xff,0xff,0xff,0xff,0,0};
//{0x00,0x23,0x45,0x67,0x89,0xb9};
const u32_t receive_window_lower_bound=40960;
const u32_t receive_window_random_range=512;
const unsigned char wscale=0x05;
char g_packet_buf[buf_len]; //looks dirty but works well
int g_packet_buf_len=-1;
int g_packet_buf_cnt=0;
union
{
sockaddr_ll ll;
sockaddr_in ipv4;
sockaddr_in6 ipv6;
}g_sockaddr;
socklen_t g_sockaddr_len = -1;
struct sock_filter code_tcp_old[] = {
{ 0x28, 0, 0, 0x0000000c },//0
{ 0x15, 0, 10, 0x00000800 },//1
{ 0x30, 0, 0, 0x00000017 },//2
{ 0x15, 0, 8, 0x00000006 },//3
{ 0x28, 0, 0, 0x00000014 },//4
{ 0x45, 6, 0, 0x00001fff },//5
{ 0xb1, 0, 0, 0x0000000e },//6
{ 0x48, 0, 0, 0x0000000e },//7
{ 0x15, 2, 0, 0x0000ef32 },//8
{ 0x48, 0, 0, 0x00000010 },//9
{ 0x15, 0, 1, 0x0000ef32 },//10
{ 0x6, 0, 0, 0x0000ffff },//11
{ 0x6, 0, 0, 0x00000000 },//12
};
struct sock_filter code_tcp[] = {
//{ 0x5, 0, 0, 0x00000001 },//0 //jump to 2,dirty hack from tcpdump -d's output
//{ 0x5, 0, 0, 0x00000000 },//1
{ 0x30, 0, 0, 0x00000009 },//2
{ 0x15, 0, 6, 0x00000006 },//3
{ 0x28, 0, 0, 0x00000006 },//4
{ 0x45, 4, 0, 0x00001fff },//5
{ 0xb1, 0, 0, 0x00000000 },//6
{ 0x48, 0, 0, 0x00000002 },//7
{ 0x15, 0, 1, 0x0000fffe },//8 //modify this fffe to the port you listen on
{ 0x6, 0, 0, 0x0000ffff },//9
{ 0x6, 0, 0, 0x00000000 },//10
};
int code_tcp_port_index=6;
struct sock_filter code_udp[] = {
//{ 0x5, 0, 0, 0x00000001 },
//{ 0x5, 0, 0, 0x00000000 },
{ 0x30, 0, 0, 0x00000009 },
{ 0x15, 0, 6, 0x00000011 },
{ 0x28, 0, 0, 0x00000006 },
{ 0x45, 4, 0, 0x00001fff },
{ 0xb1, 0, 0, 0x00000000 },
{ 0x48, 0, 0, 0x00000002 },
{ 0x15, 0, 1, 0x0000fffe }, //modify this fffe to the port you listen on
{ 0x6, 0, 0, 0x0000ffff },
{ 0x6, 0, 0, 0x00000000 },
};
int code_udp_port_index=6;
struct sock_filter code_icmp[] = {
//{ 0x5, 0, 0, 0x00000001 },
//{ 0x5, 0, 0, 0x00000000 },
{ 0x30, 0, 0, 0x00000009 },
{ 0x15, 0, 1, 0x00000001 },
{ 0x6, 0, 0, 0x0000ffff },
{ 0x6, 0, 0, 0x00000000 },
};
/*
tcpdump -i eth1 ip and icmp -d
(000) ldh [12]
(001) jeq #0x800 jt 2 jf 5
(002) ldb [23]
(003) jeq #0x1 jt 4 jf 5
(004) ret #65535
(005) ret #0
tcpdump -i eth1 ip and icmp -dd
{ 0x28, 0, 0, 0x0000000c },
{ 0x15, 0, 3, 0x00000800 },
{ 0x30, 0, 0, 0x00000017 },
{ 0x15, 0, 1, 0x00000001 },
{ 0x6, 0, 0, 0x0000ffff },
{ 0x6, 0, 0, 0x00000000 },
*/
/*
tcpdump -i eth1 ip and tcp and dst port 65534 -dd
{ 0x28, 0, 0, 0x0000000c },
{ 0x15, 0, 8, 0x00000800 },
{ 0x30, 0, 0, 0x00000017 },
{ 0x15, 0, 6, 0x00000006 },
{ 0x28, 0, 0, 0x00000014 },
{ 0x45, 4, 0, 0x00001fff },
{ 0xb1, 0, 0, 0x0000000e },
{ 0x48, 0, 0, 0x00000010 },
{ 0x15, 0, 1, 0x0000fffe },
{ 0x6, 0, 0, 0x0000ffff },
{ 0x6, 0, 0, 0x00000000 },
(000) ldh [12]
(001) jeq #0x800 jt 2 jf 10
(002) ldb [23]
(003) jeq #0x6 jt 4 jf 10
(004) ldh [20]
(005) jset #0x1fff jt 10 jf 6
(006) ldxb 4*([14]&0xf)
(007) ldh [x + 16]
(008) jeq #0xfffe jt 9 jf 10
(009) ret #65535
(010) ret #0
*/
packet_info_t::packet_info_t()
{
src_port=0;
dst_port=0;
if (raw_mode == mode_faketcp)
{
protocol = IPPROTO_TCP;
ack_seq = get_true_random_number();
seq = get_true_random_number();
has_ts=0;
ts_ack=0;
syn=0;
ack=1;
ack_seq_counter=0;
//mylog(log_info,"<cons ,ts_ack= %u>\n",ts_ack);
}
else if (raw_mode == mode_udp)
{
protocol = IPPROTO_UDP;
}
else if (raw_mode == mode_icmp)
{
protocol = IPPROTO_ICMP;
icmp_seq=0;
}
}
int init_raw_socket()
{
g_ip_id_counter=get_true_random_number()%65535;
if(lower_level==0)
{
raw_send_fd = socket(AF_INET , SOCK_RAW , IPPROTO_RAW);// IPPROTO_TCP??
if(raw_send_fd == -1) {
mylog(log_fatal,"Failed to create raw_send_fd\n");
//perror("Failed to create raw_send_fd");
myexit(1);
}
/*
int one = 1;
const int *val = &one;
if (setsockopt (raw_send_fd, IPPROTO_IP, IP_HDRINCL, val, sizeof (one)) < 0) {
mylog(log_fatal,"Error setting IP_HDRINCL %d\n",errno);
//perror("Error setting IP_HDRINCL");
myexit(2);
}*/
}
else
{
raw_send_fd = socket(PF_PACKET , SOCK_DGRAM , htons(ETH_P_IP));// todo how to create a recv only raw socket?
if(raw_send_fd == -1) {
mylog(log_fatal,"Failed to create raw_send_fd\n");
//perror("Failed to create raw_send_fd");
myexit(1);
}
//init_ifindex(if_name);
}
int opt = 0;
assert(setsockopt(raw_send_fd, SOL_SOCKET, SO_RCVBUF, &opt, sizeof(opt))==0);// raw_send_fd is for send only, set its recv buffer to zero
if(force_socket_buf)
{
if(setsockopt(raw_send_fd, SOL_SOCKET, SO_SNDBUFFORCE, &socket_buf_size, sizeof(socket_buf_size))<0)
{
mylog(log_fatal,"SO_SNDBUFFORCE fail socket_buf_size=%d errno=%s\n",socket_buf_size,strerror(errno));
myexit(1);
}
}
else
{
if(setsockopt(raw_send_fd, SOL_SOCKET, SO_SNDBUF, &socket_buf_size, sizeof(socket_buf_size))<0)
{
mylog(log_fatal,"SO_SNDBUF fail socket_buf_size=%d errno=%s\n",socket_buf_size,strerror(errno));
myexit(1);
}
}
//raw_fd = socket(AF_PACKET, SOCK_DGRAM, htons(ETH_P_ALL));
raw_recv_fd= socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_IP));
//ETH_P_IP doesnt read outgoing packets
// https://stackoverflow.com/questions/20264895/eth-p-ip-is-not-working-as-expected-i-can-only-receive-incoming-packets
// to capture both incoming and outgoing packets use ETH_P_ALL
if(raw_recv_fd == -1) {
mylog(log_fatal,"Failed to create raw_recv_fd\n");
//perror("");
myexit(1);
}
if(strlen(dev)!=0)
{
struct sockaddr_ll bind_address;
memset(&bind_address, 0, sizeof(bind_address));
int index=-1;
assert(init_ifindex(dev,raw_recv_fd,index)==0);
bind_address.sll_family = AF_PACKET;
bind_address.sll_protocol = htons(ETH_P_ALL);
bind_address.sll_ifindex = index;
if(bind(raw_recv_fd, (struct sockaddr *)&bind_address, sizeof(bind_address))==-1)
{
mylog(log_fatal,"bind to dev [%s] failed\n",dev);
myexit(1);
}
}
if(force_socket_buf)
{
if(setsockopt(raw_recv_fd, SOL_SOCKET, SO_RCVBUFFORCE, &socket_buf_size, sizeof(socket_buf_size))<0)
{
mylog(log_fatal,"SO_RCVBUFFORCE fail socket_buf_size=%d errno=%s\n",socket_buf_size,strerror(errno));
myexit(1);
}
}
else
{
if(setsockopt(raw_recv_fd, SOL_SOCKET, SO_RCVBUF, &socket_buf_size, sizeof(socket_buf_size))<0)
{
mylog(log_fatal,"SO_RCVBUF fail socket_buf_size=%d errno=%s\n",socket_buf_size,strerror(errno));
myexit(1);
}
}
//IP_HDRINCL to tell the kernel that headers are included in the packet
setnonblocking(raw_send_fd); //not really necessary
setnonblocking(raw_recv_fd);
return 0;
}
void init_filter(int port)
{
sock_fprog bpf;
if(raw_mode==mode_faketcp||raw_mode==mode_udp)
{
filter_port=port;
}
if(disable_bpf_filter) return;
//if(raw_mode==mode_icmp) return ;
//code_tcp[8].k=code_tcp[10].k=port;
if(raw_mode==mode_faketcp)
{
bpf.len = sizeof(code_tcp)/sizeof(code_tcp[0]);
code_tcp[code_tcp_port_index].k=port;
bpf.filter = code_tcp;
}
else if(raw_mode==mode_udp)
{
bpf.len = sizeof(code_udp)/sizeof(code_udp[0]);
code_udp[code_udp_port_index].k=port;
bpf.filter = code_udp;
}
else if(raw_mode==mode_icmp)
{
bpf.len = sizeof(code_icmp)/sizeof(code_icmp[0]);
bpf.filter = code_icmp;
}
int dummy;
int ret=setsockopt(raw_recv_fd, SOL_SOCKET, SO_DETACH_FILTER, &dummy, sizeof(dummy)); //in case i forgot to remove
if (ret != 0)
{
mylog(log_debug,"error remove fiter\n");
//perror("filter");
//exit(-1);
}
ret = setsockopt(raw_recv_fd, SOL_SOCKET, SO_ATTACH_FILTER, &bpf, sizeof(bpf));
if (ret != 0)
{
mylog(log_fatal,"error set fiter\n");
//perror("filter");
myexit(-1);
}
}
void remove_filter()
{
filter_port=0;
int dummy;
int ret=setsockopt(raw_recv_fd, SOL_SOCKET, SO_DETACH_FILTER, &dummy, sizeof(dummy));
if (ret != 0)
{
mylog(log_debug,"error remove fiter\n");
//perror("filter");
//exit(-1);
}
}
int init_ifindex(const char * if_name,int fd,int &index)
{
struct ifreq ifr;
size_t if_name_len=strlen(if_name);
if (if_name_len<sizeof(ifr.ifr_name)) {
memcpy(ifr.ifr_name,if_name,if_name_len);
ifr.ifr_name[if_name_len]=0;
} else {
mylog(log_fatal,"interface name is too long\n");
myexit(-1);
}
if (ioctl(fd,SIOCGIFINDEX,&ifr)==-1) {
mylog(log_fatal,"SIOCGIFINDEX fail ,%s\n",strerror(errno));
myexit(-1);
}
index=ifr.ifr_ifindex;
mylog(log_info,"ifname:%s ifindex:%d\n",if_name,index);
return 0;
}
bool interface_has_arp(const char * interface) {
struct ifreq ifr;
// int sock = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
int sock=raw_send_fd;
memset(&ifr, 0, sizeof(ifr));
strcpy(ifr.ifr_name, interface);
if (ioctl(sock, SIOCGIFFLAGS, &ifr) < 0) {
//perror("SIOCGIFFLAGS");
mylog(log_fatal,"ioctl(sock, SIOCGIFFLAGS, &ifr) failed for interface %s,errno %s\n",interface,strerror(errno));
myexit(-1);
}
//close(sock);
return !(ifr.ifr_flags & IFF_NOARP);
}
struct route_info_t
{
string if_name;
u32_t dest;
u32_t mask;
u32_t gw;
u32_t flag;
};
int dest_idx=1;
int gw_idx=2;
int if_idx=0;
int mask_idx=7;
int flag_idx=3;
vector<int> find_route_entry(const vector<route_info_t> &route_info_vec,u32_t ip)
{
vector<int> res;
for(u32_t i=0;i<=32;i++)
{
u32_t mask=0xffffffff;
//mask >>=i;
//if(i==32) mask=0; //why 0xffffffff>>32 equals 0xffffffff??
mask <<=i;
if(i==32) mask=0;
log_bare(log_debug,"(mask:%x)",mask);
for(u32_t j=0;j<route_info_vec.size();j++)
{
const route_info_t & info=route_info_vec[j];
if(info.mask!=mask)
continue;
log_bare(log_debug,"<<%d,%d>>",i,j);
if((info.dest&mask)==(ip&mask))
{
log_bare(log_debug,"found!");
res.push_back(j);
}
}
if(res.size()!=0)
{
return res;
}
}
return res;
}
int find_direct_dest(const vector<route_info_t> &route_info_vec,u32_t ip,u32_t &dest_ip,string &if_name)
{
vector<int> res;
for(int i=0;i<1000;i++)
{
res=find_route_entry(route_info_vec,ip);
log_bare(log_debug,"<entry:%u>",(u32_t)res.size());
if(res.size()==0)
{
mylog(log_error,"cant find route entry\n");
return -1;
}
if(res.size()>1)
{
mylog(log_error,"found duplicated entries\n");
return -1;
}
if((route_info_vec[res[0]].flag&2)==0)
{
dest_ip=ip;
if_name=route_info_vec[res[0]].if_name;
return 0;
}
else
{
ip=route_info_vec[res[0]].gw;
}
}
mylog(log_error,"dead loop in find_direct_dest\n");
return -1;
}
struct arp_info_t
{
u32_t ip;
string hw;
string if_name;
};
int arp_ip_idx=0;
int arp_hw_idx=3;
int arp_if_idx=5;
int find_arp(const vector<arp_info_t> &arp_info_vec,u32_t ip,string if_name,string &hw)
{
int pos=-1;
int count=0;
for(u32_t i=0;i<arp_info_vec.size();i++)
{
const arp_info_t & info=arp_info_vec[i];
if(info.if_name!=if_name) continue;
if(info.ip==ip)
{
count++;
pos=i;
}
}
if(count==0)
{
//mylog(log_warn,"cant find arp entry for %s %s,using 00:00:00:00:00:00\n",my_ntoa(ip),if_name.c_str());
//hw="00:00:00:00:00:00";
mylog(log_error,"cant find arp entry for %s %s\n",my_ntoa(ip),if_name.c_str());
return -1;
}
if(count>1)
{
mylog(log_error,"find multiple arp entry for %s %s\n",my_ntoa(ip),if_name.c_str());
return -1;
}
hw=arp_info_vec[pos].hw;
return 0;
}
int find_lower_level_info(u32_t ip,u32_t &dest_ip,string &if_name,string &hw)
{
ip=htonl(ip);
if(ip==htonl(inet_addr("127.0.0.1")))
{
dest_ip=ntohl(ip);
if_name="lo";
hw="00:00:00:00:00:00";
return 0;
}
string route_file;
if(read_file("/proc/net/route",route_file)!=0) return -1;
string arp_file;
if(read_file("/proc/net/arp",arp_file)!=0) return -1;
log_bare(log_debug,"/proc/net/route:<<%s>>\n",route_file.c_str());
log_bare(log_debug,"/proc/net/arp:<<%s>>\n",route_file.c_str());
auto route_vec2=string_to_vec2(route_file.c_str());
vector<route_info_t> route_info_vec;
for(u32_t i=1;i<route_vec2.size();i++)
{
log_bare(log_debug,"<size:%u>",(u32_t)route_vec2[i].size());
if(route_vec2[i].size()!=11)
{
mylog(log_error,"route coloum %d !=11 \n",int(route_vec2[i].size()));
return -1;
}
route_info_t tmp;
tmp.if_name=route_vec2[i][if_idx];
if(hex_to_u32_with_endian(route_vec2[i][dest_idx],tmp.dest)!=0) return -1;
if(hex_to_u32_with_endian(route_vec2[i][gw_idx],tmp.gw)!=0) return -1;
if(hex_to_u32_with_endian(route_vec2[i][mask_idx],tmp.mask)!=0) return -1;
if(hex_to_u32(route_vec2[i][flag_idx],tmp.flag)!=0)return -1;
route_info_vec.push_back(tmp);
for(u32_t j=0;j<route_vec2[i].size();j++)
{
log_bare(log_debug,"<%s>",route_vec2[i][j].c_str());
}
log_bare(log_debug,"%s dest:%x mask:%x gw:%x flag:%x",tmp.if_name.c_str(),tmp.dest,tmp.mask,tmp.gw,tmp.flag);
log_bare(log_debug,"\n");
}
if(find_direct_dest(route_info_vec,ip,dest_ip,if_name)!=0)
{
mylog(log_error,"find_direct_dest failed for ip %s\n",my_ntoa(ntohl(ip)));
return -1;
}
log_bare(log_debug,"========\n");
auto arp_vec2=string_to_vec2(arp_file.c_str());
vector<arp_info_t> arp_info_vec;
for(u32_t i=1;i<arp_vec2.size();i++)
{
log_bare(log_debug,"<<arp_vec2[i].size(): %d>>",(int)arp_vec2[i].size());
for(u32_t j=0;j<arp_vec2[i].size();j++)
{
log_bare(log_debug,"<%s>",arp_vec2[i][j].c_str());
}
if(arp_vec2[i].size()!=6)
{
mylog(log_error,"arp coloum %d !=11 \n",int(arp_vec2[i].size()));
return -1;
}
arp_info_t tmp;
tmp.if_name=arp_vec2[i][arp_if_idx];
tmp.hw=arp_vec2[i][arp_hw_idx];
tmp.ip=htonl(inet_addr(arp_vec2[i][arp_ip_idx].c_str()));
arp_info_vec.push_back(tmp);
log_bare(log_debug,"\n");
}
if(!interface_has_arp(if_name.c_str()))
{
mylog(log_info,"%s is a noarp interface,using 00:00:00:00:00:00\n",if_name.c_str());
hw="00:00:00:00:00:00";
}
else if(find_arp(arp_info_vec,dest_ip,if_name,hw)!=0)
{
mylog(log_error,"find_arp failed for dest_ip %s ,if_name %s\n",my_ntoa(ntohl(ip)),if_name.c_str());
return -1;
}
//printf("%s\n",hw.c_str());
dest_ip=ntohl(dest_ip);
return 0;
}
int send_raw_ip(raw_info_t &raw_info,const char * payload,int payloadlen)
{
const packet_info_t &send_info=raw_info.send_info;
const packet_info_t &recv_info=raw_info.recv_info;
char send_raw_ip_buf[buf_len];
if(raw_info.disabled)
{
mylog(log_debug,"[%s,%d]connection disabled, no packet will be sent\n",my_ntoa(recv_info.src_ip),recv_info.src_port);
assert(max_rst_allowed>=0);
return 0;
}
struct iphdr *iph = (struct iphdr *) send_raw_ip_buf;
memset(iph,0,sizeof(iphdr));
iph->ihl = sizeof(iphdr)/4; //we dont use ip options,so the length is just sizeof(iphdr)
iph->version = 4;
iph->tos = 0;
if(lower_level)
{
//iph->id=0;
iph->id = htons (g_ip_id_counter++); //Id of this packet
}
else
iph->id = htons (g_ip_id_counter++); //Id of this packet
//iph->id = 0; //Id of this packet ,kernel will auto fill this if id is zero ,or really?????// todo //seems like there is a problem
iph->frag_off = htons(0x4000); //DF set,others are zero
// iph->frag_off = htons(0x0000); //DF set,others are zero
iph->ttl = (unsigned char)ttl_value;
iph->protocol = send_info.protocol;
iph->check = 0; //Set to 0 before calculating checksum
iph->saddr = send_info.src_ip; //Spoof the source ip address
iph->daddr = send_info.dst_ip;
uint16_t ip_tot_len=sizeof (struct iphdr)+payloadlen;
if(lower_level)iph->tot_len = htons(ip_tot_len); //this is not necessary ,kernel will always auto fill this //http://man7.org/linux/man-pages/man7/raw.7.html
else
iph->tot_len = 0;
memcpy(send_raw_ip_buf+sizeof(iphdr) , payload, payloadlen);
if(lower_level) iph->check =
csum ((unsigned short *) send_raw_ip_buf, iph->ihl*4); //this is not necessary ,kernel will always auto fill this
else
iph->check=0;
int ret;
if(lower_level==0)
{
struct sockaddr_in sin={0};
sin.sin_family = AF_INET;
//sin.sin_port = htons(info.dst_port); //dont need this
sin.sin_addr.s_addr = send_info.dst_ip;
ret = sendto(raw_send_fd, send_raw_ip_buf, ip_tot_len , 0, (struct sockaddr *) &sin, sizeof (sin));
}
else
{
struct sockaddr_ll addr={0}; //={0} not necessary
memcpy(&addr,&send_info.addr_ll,sizeof(addr));
ret = sendto(raw_send_fd, send_raw_ip_buf, ip_tot_len , 0, (struct sockaddr *) &addr, sizeof (addr));
}
if(ret==-1)
{
mylog(log_trace,"sendto failed\n");
//perror("why?");
return -1;
}
else
{
//mylog(log_info,"sendto succ\n");
}
return 0;
}
int peek_raw(packet_info_t &peek_info)
{
//static char peek_raw_buf[buf_len];
assert(g_packet_buf_cnt==1);
//g_packet_buf_cnt--;
char * peek_raw_buf=g_packet_buf;
int recv_len=g_packet_buf_len;
char *ip_begin=peek_raw_buf+link_level_header_len;
//struct sockaddr saddr={0};
//socklen_t saddr_size=sizeof(saddr);
//int recv_len = recvfrom(raw_recv_fd, peek_raw_buf,max_data_len, MSG_PEEK ,&saddr , &saddr_size);//change max_data_len to something smaller,we only need header here
iphdr * iph = (struct iphdr *) (ip_begin);
//mylog(log_info,"recv_len %d\n",recv_len);
if(recv_len<int(sizeof(iphdr)))
{
mylog(log_trace,"failed here %d %d\n",recv_len,int(sizeof(iphdr)));
mylog(log_trace,"%s\n ",strerror(errno));
return -1;
}
peek_info.src_ip=iph->saddr;
unsigned short iphdrlen =iph->ihl*4;
char *payload=ip_begin+iphdrlen;
//mylog(log_info,"protocol %d\n",iph->protocol);
switch(raw_mode)
{
case mode_faketcp:
{
if(iph->protocol!=IPPROTO_TCP)
{
mylog(log_trace,"failed here");
return -1;
}
struct tcphdr *tcph=(tcphdr *)payload;
if(recv_len<int( iphdrlen+sizeof(tcphdr) ))
{
mylog(log_trace,"failed here");
return -1;
}
peek_info.src_port=ntohs(tcph->source);
peek_info.syn=tcph->syn;
break;
}
case mode_udp:
{
if(iph->protocol!=IPPROTO_UDP) return -1;
struct udphdr *udph=(udphdr *)payload;
if(recv_len<int(iphdrlen+sizeof(udphdr)))
return -1;
peek_info.src_port=ntohs(udph->source);
break;
}
case mode_icmp:
{
if(iph->protocol!=IPPROTO_ICMP) return -1;
struct icmphdr *icmph=(icmphdr *)payload;
if(recv_len<int( iphdrlen+sizeof(icmphdr) ))
return -1;
peek_info.src_port=ntohs(icmph->id);
break;
}
default:return -1;
}
return 0;
}
int pre_recv_raw_packet()
{
assert(g_packet_buf_cnt==0);
g_sockaddr_len=sizeof(g_sockaddr.ll);
g_packet_buf_len = recvfrom(raw_recv_fd, g_packet_buf, max_data_len+1, 0 ,(sockaddr*)&g_sockaddr , &g_sockaddr_len);
//assert(g_sockaddr_len==sizeof(g_sockaddr.ll)); //g_sockaddr_len=18, sizeof(g_sockaddr.ll)=20, why its not equal? maybe its bc sll_halen is 6?
//assert(g_addr_ll_size==sizeof(g_addr_ll));
if(g_packet_buf_len==max_data_len+1)
{
mylog(log_warn,"huge packet, data_len > %d,dropped\n",max_data_len);
return -1;
}
if(g_packet_buf_len<0)
{
mylog(log_trace,"recv_len %d\n",g_packet_buf_len);
return -1;
}
g_packet_buf_cnt++;
return 0;
}
int discard_raw_packet()
{
assert(g_packet_buf_cnt==1);
g_packet_buf_cnt--;
return 0;
}
int recv_raw_ip(raw_info_t &raw_info,char * &payload,int &payloadlen)
{
assert(g_packet_buf_cnt==1);
g_packet_buf_cnt--;
char *recv_raw_ip_buf=g_packet_buf;
//static char recv_raw_ip_buf[buf_len];
int recv_len=g_packet_buf_len;
const packet_info_t &send_info=raw_info.send_info;
packet_info_t &recv_info=raw_info.recv_info;
iphdr * iph;
int flag=0;
//int recv_len = recvfrom(raw_recv_fd, recv_raw_ip_buf, max_data_len+1, flag ,(sockaddr*)&saddr , &saddr_size);
if(recv_len<int(link_level_header_len))
{
mylog(log_trace,"length error\n");
}
if(link_level_header_len ==14&&(recv_raw_ip_buf[12]!=8||recv_raw_ip_buf[13]!=0))
{
mylog(log_trace,"not an ipv4 packet!\n");
return -1;
}
char *ip_begin=recv_raw_ip_buf+link_level_header_len; //14 is eth net header
iph = (struct iphdr *) (ip_begin);
recv_info.src_ip=iph->saddr;
recv_info.dst_ip=iph->daddr;
recv_info.protocol=iph->protocol;
if(lower_level)
{
memcpy(&recv_info.addr_ll,&g_sockaddr.ll,sizeof(recv_info.addr_ll));
}
if(bind_addr_used && recv_info.dst_ip!=bind_addr.inner.ipv4.sin_addr.s_addr)
{
mylog(log_trace,"bind adress doenst match, dropped\n");
//printf(" bind adress doenst match, dropped\n");
return -1;
}
if (!(iph->ihl > 0 && iph->ihl <=60)) {
mylog(log_trace,"iph ihl error\n");
return -1;
}
int ip_len=ntohs(iph->tot_len);
if(recv_len-int(link_level_header_len) <ip_len)
{
mylog(log_debug,"incomplete packet\n");
return -1;
}
unsigned short iphdrlen =iph->ihl*4;
u32_t ip_chk=csum ((unsigned short *) ip_begin, iphdrlen);
if(ip_chk!=0)
{
mylog(log_debug,"ip header error %x\n",ip_chk);
return -1;
}
payload=ip_begin+iphdrlen;
payloadlen=ip_len-iphdrlen;
if(payloadlen<0)
{
mylog(log_warn,"error payload len\n");
return -1;
}
return 0;
}
int send_raw_icmp(raw_info_t &raw_info, const char * payload, int payloadlen)
{
const packet_info_t &send_info=raw_info.send_info;
const packet_info_t &recv_info=raw_info.recv_info;
char send_raw_icmp_buf[buf_len];
icmphdr *icmph=(struct icmphdr *) (send_raw_icmp_buf);
memset(icmph,0,sizeof(icmphdr));
if(program_mode==client_mode)
{
icmph->type=8;
}
else
{
icmph->type=0;
}
icmph->code=0;
icmph->id=htons(send_info.src_port);
icmph->seq=htons(send_info.icmp_seq); /////////////modify
memcpy(send_raw_icmp_buf+sizeof(icmphdr),payload,payloadlen);
icmph->check_sum = csum( (unsigned short*) send_raw_icmp_buf, sizeof(icmphdr)+payloadlen);
if(send_raw_ip(raw_info,send_raw_icmp_buf,sizeof(icmphdr)+payloadlen)!=0)
{
return -1;
}
/*if(program_mode==client_mode)
{
send_info.icmp_seq++;
}*/
return 0;
}
int send_raw_udp(raw_info_t &raw_info, const char * payload, int payloadlen)
{
const packet_info_t &send_info=raw_info.send_info;
const packet_info_t &recv_info=raw_info.recv_info;
char send_raw_udp_buf[buf_len];
udphdr *udph=(struct udphdr *) (send_raw_udp_buf);
memset(udph,0,sizeof(udphdr));
struct pseudo_header tmp_header={0};
struct pseudo_header *psh = &tmp_header;
udph->source = htons(send_info.src_port);
udph->dest = htons(send_info.dst_port);
int udp_tot_len=payloadlen+sizeof(udphdr);
if(udp_tot_len>65535)
{
mylog(log_debug,"invalid len\n");
return -1;
}
mylog(log_trace,"udp_len:%d %d\n",udp_tot_len,udph->len);
udph->len=htons(uint16_t(udp_tot_len));
memcpy(send_raw_udp_buf+sizeof(udphdr),payload,payloadlen);
psh->source_address = send_info.src_ip;
psh->dest_address = send_info.dst_ip;
psh->placeholder = 0;
psh->protocol = IPPROTO_UDP;
psh->tcp_length = htons(uint16_t(udp_tot_len));
int csum_size = udp_tot_len ;
udph->check = csum_with_header((char *)psh,sizeof(pseudo_header), (unsigned short*) send_raw_udp_buf, csum_size);
if(send_raw_ip(raw_info,send_raw_udp_buf,udp_tot_len)!=0)
{
return -1;
}
return 0;
}
int send_raw_tcp(raw_info_t &raw_info,const char * payload, int payloadlen) { //TODO seq increase
const packet_info_t &send_info=raw_info.send_info;
const packet_info_t &recv_info=raw_info.recv_info;
//mylog(log_debug,"syn %d\n",send_info.syn);
char send_raw_tcp_buf[buf_len];
//char *send_raw_tcp_buf=send_raw_tcp_buf0;
struct tcphdr *tcph = (struct tcphdr *) (send_raw_tcp_buf);
memset(tcph,0,sizeof(tcphdr));
pseudo_header tmp_header={0};
struct pseudo_header *psh = &tmp_header;
//TCP Header
tcph->source = htons(send_info.src_port);
tcph->dest = htons(send_info.dst_port);
tcph->seq = htonl(send_info.seq);
tcph->ack_seq = htonl(send_info.ack_seq);
tcph->fin = 0;
tcph->syn = send_info.syn;
tcph->rst = 0;
tcph->psh = send_info.psh;
tcph->ack = send_info.ack;
if (tcph->syn == 1) {
tcph->doff = 10; //tcp header size
int i = sizeof(tcphdr);
send_raw_tcp_buf[i++] = 0x02; //mss
send_raw_tcp_buf[i++] = 0x04;
send_raw_tcp_buf[i++] = 0x05;
send_raw_tcp_buf[i++] = (char)0xb4;
//raw_send_buf[i++]=0x01;
//raw_send_buf[i++]=0x01;
send_raw_tcp_buf[i++] = 0x04; //sack ok
send_raw_tcp_buf[i++] = 0x02; //sack ok
send_raw_tcp_buf[i++] = 0x08; //ts i=6
send_raw_tcp_buf[i++] = 0x0a; //i=7
//*(u32_t*) (&send_raw_tcp_buf[i]) = htonl(