-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathelfprobe.c
1744 lines (1538 loc) · 51.4 KB
/
elfprobe.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
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
/*
* elfprobe.c: main eBPF code for ELF
*/
#define BPF_LICENSE GPL
#define KBUILD_MODNAME "foo"
// largely copies of linux header definitions
// to avoid any #includes and lack of kernel headers
#define ETH_ALEN 6
typedef uint32_t _in_addr_t;
typedef struct in6_addr {
union {
uint32_t _addr32[4];
uint16_t _addr16[8];
uint8_t _addr8[16];
} _u;
} _in6_addr_t;
struct _ethhdr {
uint8_t ether_dhost[ETH_ALEN]; /* destination eth addr */
uint8_t ether_shost[ETH_ALEN]; /* source ether addr */
uint16_t ether_type; /* packet type ID field */
} __attribute__ ((__packed__));
struct _iphdr {
uint8_t verihl;
uint8_t tos;
uint16_t tot_len;
uint16_t id;
uint16_t frag_off;
uint8_t ttl;
uint8_t protocol;
uint16_t check;
uint32_t saddr;
uint32_t daddr;
/*The options start here. */
};
struct _ip6hdr {
uint32_t ip6_un1_flow; /* 4 bits version, 8 bits TC,
20 bits flow-ID */
uint16_t payload_length;/* payload length */
uint8_t protocol; /* next header */
uint8_t hop_limit; /* hop limit */
_in6_addr_t saddr; /* source address */
_in6_addr_t daddr; /* destination address */
};
struct _udphdr {
uint16_t uh_sport; /* source port */
uint16_t uh_dport; /* destination port */
uint16_t uh_ulen; /* udp length */
uint16_t uh_sum; /* udp checksum */
};
struct _tcphdr {
uint16_t th_sport; /* source port */
uint16_t th_dport; /* destination port */
uint32_t th_seq; /* sequence number */
uint32_t th_ack; /* acknowledgement number */
uint8_t th_off;
uint8_t th_flags;
# define TH_FIN 0x01
# define TH_SYN 0x02
# define TH_RST 0x04
# define TH_PUSH 0x08
# define TH_ACK 0x10
# define TH_URG 0x20
uint16_t th_win; /* window */
uint16_t th_sum; /* checksum */
uint16_t th_urp; /* urgent pointer */
};
struct _icmphdr {
uint8_t icmp_type; /* type field */
uint8_t icmp_code; /* code field */
uint16_t icmp_cksum; /* checksum field */
uint8_t icmp_reserved[4];
};
#define IPPROTO_ICMP 1
#define IPPROTO_TCP 6
#define IPPROTO_UDP 17
#define IPPROTO_ICMP6 58
#define ETHERTYPE_IP 0x0800
#define ETHERTYPE_IP6 0x86dd
#define ICMP6_DST_UNREACH 1
#define ICMP6_TIME_EXCEEDED 3
#define ICMP6_ECHO_REQUEST 128
#define ICMP6_ECHO_REPLY 129
#define ICMP_ECHO_REPLY 0 /* Echo Reply */
#define ICMP_DEST_UNREACH 3 /* Destination Unreachable */
#define ICMP_ECHO_REQUEST 8 /* Echo Request */
#define ICMP_TIME_EXCEEDED 11 /* Time Exceeded */
#ifndef TC_ACT_OK
#define TC_ACT_OK 0
#define TC_ACT_SHOT 2
#endif
#ifndef FALSE
#define FALSE 0
#define TRUE 1
#endif
#define MAXDEST 128
#define MAXRESULTS 8192
#define IP_TTL_OFF offsetof(struct _iphdr, ttl)
#define IP_SRC_OFF offsetof(struct _iphdr, saddr)
#define IP_DST_OFF offsetof(struct _iphdr, daddr)
#define IP_LEN_OFF offsetof(struct _iphdr, tot_len)
#define IP_ID_OFF offsetof(struct _iphdr, id)
#define IP_CSUM_OFF offsetof(struct _iphdr, check)
#define IP6_ID_OFF 2
#define IP6_DST_OFF offsetof(struct _ip6hdr, daddr)
#define IP6_SRC_OFF offsetof(struct _ip6hdr, saddr)
#define IP6_TTL_OFF offsetof(struct _ip6hdr, hop_limit)
#define IP6_LEN_OFF offsetof(struct _ip6hdr, payload_length)
#define ICMP_CSUM_OFF offsetof(struct _icmphdr, icmp_cksum)
#define ICMP_TYPE_OFF offsetof(struct _icmphdr, icmp_type)
#define ICMP_ID_OFF offsetof(struct _icmphdr, icmp_reserved[0])
#define ICMP_SEQ_OFF offsetof(struct _icmphdr, icmp_reserved[2])
#define TCP_SRC_OFF offsetof(struct _tcphdr, th_sport)
#define TCP_DST_OFF offsetof(struct _tcphdr, th_dport)
#define TCP_SEQ_OFF offsetof(struct _tcphdr, th_seq)
#define TCP_ACK_OFF offsetof(struct _tcphdr, th_ack)
#define TCP_CSUM_OFF offsetof(struct _tcphdr, th_sum)
#define UDP_SRC_OFF offsetof(struct _udphdr, uh_sport)
#define UDP_DST_OFF offsetof(struct _udphdr, uh_dport)
#define UDP_CSUM_OFF offsetof(struct _udphdr, uh_sum)
struct probe_dest {
u32 hop_bitmap;
u16 sequence;
u16 next_hop_to_probe;
u16 maxttl;
u16 pad;
u64 last_send;
u64 last_mttl_update;
_in6_addr_t dest;
};
struct sent_info {
u64 send_time;
_in6_addr_t dest;
u16 sport;
u16 dport;
u32 origseq;
u8 outttl;
u8 protocol;
u16 outipid;
};
struct latency_sample {
u16 sequence;
u16 outipid;
u16 inipid;
u16 pad1;
u32 origseq;
u64 send;
u64 recv;
u16 sport;
u16 dport;
u8 outttl;
u8 recvttl;
u8 protocol;
u8 pad2;
_in6_addr_t responder;
_in6_addr_t target;
};
BPF_PROG_ARRAY(ingress_layer3, 8);
BPF_PROG_ARRAY(egress_layer3, 8);
BPF_PROG_ARRAY(egress_v4_proto, 256);
BPF_PROG_ARRAY(egress_v6_proto, 256);
BPF_HASH(trie, _in6_addr_t, u64); // key: dest address
BPF_HISTOGRAM(counters, u64, 256);
BPF_ARRAY(destinfo, struct probe_dest, MAXDEST); // index: value in trie hash
BPF_HASH(sentinfo, u64, struct sent_info); // key: destid | sequence
BPF_PERCPU_ARRAY(results, struct latency_sample, MAXRESULTS); // key: index 0 in counters
BPF_PERCPU_ARRAY(resultscount, int, 1);
#include "elfhooks.c"
static inline void _update_maxttl(int idx, int ttl) {
struct probe_dest *pd = destinfo.lookup(&idx);
if (pd == NULL) {
return;
}
u64 now = bpf_ktime_get_ns();
// only update maxttl at most every second
if (now - pd->last_mttl_update < 1000000000ULL) {
return;
}
int num_hops = 16;
if (ttl > 128) {
num_hops = 255 - ttl + 1;
} else if (ttl > 64) {
num_hops = 128 - ttl + 1;
} else if (ttl > 32) {
num_hops = 64 - ttl + 1;
} else {
num_hops = 32 - ttl + 1;
}
#ifdef DEBUG
bpf_trace_printk("updated maxttl to %d\n", num_hops);
#endif
pd->maxttl = num_hops;
pd->last_mttl_update = now;
}
static inline void _decide_seq_ttl(struct probe_dest *pd, u16 *seq, u8 *ttl) {
if (pd->sequence == 0) {
pd->sequence++;
}
*seq = pd->sequence;
pd->sequence++;
u32 bitmap = pd->hop_bitmap;
u16 mttl = pd->maxttl ? (pd->maxttl-1) : 16;
bpf_trace_printk("EGRESS decide_seq_ttl mttl %d bitmap 0x%x seq %d\n", mttl, bitmap, *seq);
#pragma unroll
for (u16 i = 0; i < 8; i++) {
u16 hop = (pd->next_hop_to_probe + i) % mttl;
// select a hop if we've only probed for about 2 sec or if the
// hop has been responsive to prior probes
if ((*seq < ((pd->maxttl-1)*(2000000000ULL/PROBE_INT))) ||
((bitmap >> hop) & 0x1) == 0x1) {
*ttl = (u8)(hop + 1);
pd->next_hop_to_probe = (hop + 1) % mttl;
return;
}
}
*ttl = (u8)((pd->next_hop_to_probe % mttl) + 1);
pd->next_hop_to_probe = (pd->next_hop_to_probe + 1) % mttl;
}
static inline int _should_probe_dest(int idx) {
struct probe_dest *pd = destinfo.lookup(&idx);
if (pd == NULL) {
return FALSE;
}
if (pd->maxttl == 1) { // too short a path - just 1 hop
return FALSE;
}
// NB: PROBE_INT is in nanoseconds
#if PERHOPRATE
u64 probe_int = PROBE_INT / (u64)(pd->maxttl - 1);
#else
u64 probe_int = PROBE_INT;
#endif
u64 now = bpf_ktime_get_ns();
if ((now - pd->last_send) > probe_int) {
pd->last_send = now;
return TRUE;
}
return FALSE;
}
int egress_v4_icmp(struct __sk_buff *ctx) {
#if DEBUG
bpf_trace_printk("EGRESS icmp4 mark %d\n", ctx->mark);
#endif
int idx = ctx->mark;
struct probe_dest *pd = destinfo.lookup(&idx);
if (!pd) {
return TC_ACT_OK;
}
//
// boilerplate to get a pointer to icmphdr for cloning and
// probe generation
//
int offset = NHOFFSET;
void* data = (void*)(long)ctx->data;
void* data_end = (void*)(long)ctx->data_end;
if (data + offset + sizeof(struct _iphdr) > data_end) {
return TC_ACT_OK;
}
struct _iphdr *iph = (struct _iphdr*)(data + offset);
offset = offset + ((iph->verihl&0x0f) << 2);
struct _icmphdr *icmph = (struct _icmphdr*)(data + offset);
if (data + offset + sizeof(struct _icmphdr) > data_end) {
return TC_ACT_OK;
}
if (icmph->icmp_type != ICMP_ECHO_REQUEST) {
return TC_ACT_OK;
}
// decide what TTL to use in probe
u64 now = bpf_ktime_get_ns();
u16 origseq = load_half(ctx, offset + ICMP_SEQ_OFF);
u16 outipid = load_half(ctx, NHOFFSET + IP_ID_OFF);
u16 sport = load_half(ctx, offset + ICMP_TYPE_OFF);
u16 dport = load_half(ctx, offset + ICMP_CSUM_OFF);
u16 sequence = 0;
u8 newttl = 0;
u32 destaddr = load_word(ctx, NHOFFSET + IP_DST_OFF);
_decide_seq_ttl(pd, &sequence, &newttl);
#if DEBUG
bpf_trace_printk("EGRESS icmp4 outgoing seq %lu\n", sequence);
#endif
// clone and redirect the original pkt out the intended interface
int rv = bpf_clone_redirect(ctx, IFINDEX, 0);
if (rv < 0) {
#if DEBUG
bpf_trace_printk("EGRESS icmp4 bpf clone ifidx %d failed: %d\n", IFINDEX, rv);
#endif
// if clone fails, just let the packet pass w/o trying to do any modifications below
return TC_ACT_OK;
}
#if DEBUG
bpf_trace_printk("EGRESS icmp4 after clone emit %lu\n", sequence);
#endif
if (data + offset + sizeof(struct _icmphdr) > data_end) {
return TC_ACT_SHOT;
}
u16 old_ttl_proto = load_half(ctx, NHOFFSET + IP_TTL_OFF);
u16 new_ttl_proto = bpf_htons(((u16)newttl) << 8 | IPPROTO_ICMP);
// replace the IP checksum
rv = bpf_l3_csum_replace(ctx, NHOFFSET + IP_CSUM_OFF, bpf_htons(old_ttl_proto), new_ttl_proto, 2);
if (rv < 0) {
#if DEBUG
bpf_trace_printk("EGRESS icmp4 failed to replace csum\n");
#endif
return TC_ACT_SHOT;
}
// rewrite new IP ttl
rv = bpf_skb_store_bytes(ctx, NHOFFSET + IP_TTL_OFF, &new_ttl_proto, sizeof(new_ttl_proto), 0);
if (rv < 0) {
#if DEBUG
bpf_trace_printk("EGRESS icmp4 failed to store new ttl/proto\n");
#endif
return TC_ACT_SHOT;
}
// rewrite seq in ICMP hdr
u16 oldseq = load_half(ctx, offset + ICMP_SEQ_OFF);
u16 newseq = bpf_htons(sequence);
rv = bpf_skb_store_bytes(ctx, offset + ICMP_SEQ_OFF, &newseq, sizeof(newseq), 0);
if (rv < 0) {
#if DEBUG
bpf_trace_printk("EGRESS icmp4 failed to store new icmp seq\n");
#endif
return TC_ACT_SHOT;
}
newseq = bpf_ntohs(newseq);
// fixup ICMP checksum
u16 oldcsum = load_half(ctx, offset + ICMP_CSUM_OFF);
u16 newcsum = oldcsum - (newseq - oldseq);
newcsum = bpf_htons(newcsum);
rv = bpf_skb_store_bytes(ctx, offset + ICMP_CSUM_OFF, &newcsum, sizeof(newcsum), 0);
if (rv < 0) {
#if DEBUG
bpf_trace_printk("EGRESS icmp4 failed to store new icmp csum\n");
#endif
return TC_ACT_SHOT;
}
rv = elf_v4_icmp_beforesend(ctx);
if (rv < 0) {
#if DEBUG
bpf_trace_printk("EGRESS icmp4 beforesend fail\n");
#endif
return TC_ACT_SHOT;
}
// save info about outgoing probe into hash
// hashed on: idx|sequence
u64 sentkey = (u64)idx << 32 | (u64)sequence;
_in6_addr_t destaddr6 = {{{ destaddr, 0, 0, 0 }}};
struct sent_info si = {
.send_time = now,
.dest = destaddr6,
.sport = sport,
.dport = dport,
.origseq = origseq,
.outttl = newttl,
.protocol = IPPROTO_ICMP,
.outipid = outipid,
};
sentinfo.update(&sentkey, &si);
#if DEBUG
bpf_trace_printk("EGRESS icmp4 emitting probe %lu\n", sequence);
#endif
return TC_ACT_OK;
}
int egress_v4_tcp(struct __sk_buff *ctx) {
#if DEBUG
bpf_trace_printk("EGRESS tcp4 mark %d\n", ctx->mark);
#endif
int idx = ctx->mark;
struct probe_dest *pd = destinfo.lookup(&idx);
if (pd == NULL) {
return TC_ACT_OK;
}
//
// boilerplate to get a pointer to tcphdr for cloning and
// probe generation
//
int offset = NHOFFSET;
void* data = (void*)(long)ctx->data;
void* data_end = (void*)(long)ctx->data_end;
if (data + offset + sizeof(struct _iphdr) > data_end) {
return TC_ACT_OK;
}
struct _iphdr *iph = (struct _iphdr*)(data + offset);
int iphlen = (iph->verihl&0x0f) << 2;
offset = offset + iphlen;
if (data + offset + sizeof(struct _tcphdr) > data_end) {
return TC_ACT_OK;
}
// decide what TTL to use in probe
u16 sport = load_half(ctx, offset + TCP_SRC_OFF);
u16 dport = load_half(ctx, offset + TCP_DST_OFF);
u64 now = bpf_ktime_get_ns();
u32 origseq = bpf_ntohs(load_word(ctx, offset + TCP_SEQ_OFF));
u16 sequence = 0;
u8 newttl = 0;
u32 destaddr = load_word(ctx, NHOFFSET + IP_DST_OFF);
u16 outipid = load_half(ctx, NHOFFSET + IP_ID_OFF);
_decide_seq_ttl(pd, &sequence, &newttl);
u64 cksum64 = bpf_csum_diff(0, 0, data + NHOFFSET + IP_SRC_OFF, sizeof(u32)*2, 0);
u32 tmp = bpf_htonl(((u32)IPPROTO_TCP << 16) | 20);
cksum64 = bpf_csum_diff(0, 0, &tmp, sizeof(tmp), cksum64);
#if DEBUG
bpf_trace_printk("EGRESS tcp4 outgoing seq %lu\n", sequence);
#endif
// clone and redirect the original pkt out the intended interface
int rv = bpf_clone_redirect(ctx, IFINDEX, 0);
if (rv < 0) {
#if DEBUG
bpf_trace_printk("EGRESS tcp4 bpf clone ifidx %d failed: %d\n", IFINDEX, rv);
#endif
// if clone fails, just let the packet pass w/o trying to do any modifications below
return TC_ACT_OK;
}
#ifdef DEBUG
bpf_trace_printk("EGRESS tcp4 after clone emit %lu\n", sequence);
#endif
#ifdef NOTRUNCATE
#ifdef DEBUG
bpf_trace_printk("EGRESS tcp4 not truncating\n");
#endif
#else
struct _tcphdr newtcp;
__builtin_memset(&newtcp, 0, sizeof(struct _tcphdr));
newtcp.th_sport = bpf_htons(sport);
newtcp.th_dport = bpf_htons(dport);
newtcp.th_seq = bpf_htonl(sequence);
newtcp.th_ack = bpf_htonl(load_word(ctx, NHOFFSET + iphlen + TCP_ACK_OFF));
newtcp.th_off = 0x50;
newtcp.th_flags = TH_ACK;
// get current header values
u16 curr_ip_len = load_half(ctx, NHOFFSET + IP_LEN_OFF);
u16 new_ip_len = iphlen + sizeof(struct _tcphdr);
#if DEBUG
bpf_trace_printk("EGRESS tcp4 truncating pkt from %d to %d\n", curr_ip_len, new_ip_len);
#endif
// truncate packet
rv = bpf_skb_change_tail(ctx, NHOFFSET + new_ip_len, 0);
if (rv < 0) {
#if DEBUG
bpf_trace_printk("EGRESS tcp4 bpf trunc packet failed\n");
#endif
return TC_ACT_SHOT;
}
rv = bpf_skb_store_bytes(ctx, NHOFFSET + iphlen, &newtcp, sizeof(newtcp), 0);
if (rv < 0) {
#if DEBUG
bpf_trace_printk("EGRESS tcp4 bpf store new tcp hdr failed\n");
#endif
return TC_ACT_SHOT;
}
// add in pseudoheader words for tcp checksum
rv = bpf_l4_csum_replace(ctx, NHOFFSET + iphlen + TCP_CSUM_OFF, 0, cksum64, BPF_F_PSEUDO_HDR);
if (rv < 0) {
#if DEBUG
bpf_trace_printk("EGRESS tcp4 failed to replace csum\n");
#endif
return TC_ACT_SHOT;
}
new_ip_len = bpf_htons(new_ip_len);
curr_ip_len = bpf_htons(curr_ip_len);
rv = bpf_l3_csum_replace(ctx, NHOFFSET + IP_CSUM_OFF, curr_ip_len, new_ip_len, 2);
if (rv < 0) {
#if DEBUG
bpf_trace_printk("EGRESS tcp4 failed to replace ip csum\n");
#endif
return TC_ACT_SHOT;
}
rv = bpf_skb_store_bytes(ctx, NHOFFSET + IP_LEN_OFF, &new_ip_len, sizeof(new_ip_len), 0);
if (rv < 0) {
#if DEBUG
bpf_trace_printk("EGRESS tcp4 failed to replace ip len\n");
#endif
return TC_ACT_SHOT;
}
#endif // NOTRUNCATE
u16 old_ttl_proto = load_half(ctx, NHOFFSET + IP_TTL_OFF);
u16 new_ttl_proto = bpf_htons(((u16)newttl) << 8 | IPPROTO_TCP);
rv = bpf_l3_csum_replace(ctx, NHOFFSET + IP_CSUM_OFF, bpf_htons(old_ttl_proto), new_ttl_proto, 2);
if (rv < 0) {
#if DEBUG
bpf_trace_printk("EGRESS tcp4 failed to replace ip csum\n");
#endif
return TC_ACT_SHOT;
}
rv = bpf_skb_store_bytes(ctx, NHOFFSET + IP_TTL_OFF, &new_ttl_proto, sizeof(new_ttl_proto), 0);
if (rv < 0) {
#if DEBUG
bpf_trace_printk("EGRESS tcp4 failed to store new ttl/proto\n");
#endif
return TC_ACT_SHOT;
}
rv = elf_v4_tcp_beforesend(ctx);
if (rv < 0) {
#if DEBUG
bpf_trace_printk("EGRESS tcp4 beforesend fail\n");
#endif
return TC_ACT_SHOT;
}
// save info about outgoing probe into hash
// hashed on: idx|sequence
u64 sentkey = (u64)idx << 32 | (u64)sequence;
_in6_addr_t destaddr6 = {{{ destaddr, 0, 0, 0 }}};
struct sent_info si = {
.send_time = now,
.dest = destaddr6,
.sport = sport,
.dport = dport,
.origseq = origseq,
.outttl = newttl,
.protocol = IPPROTO_TCP,
.outipid = outipid,
};
sentinfo.update(&sentkey, &si);
#if DEBUG
bpf_trace_printk("EGRESS tcp4 emitting probe %llu\n", sequence);
#endif
return TC_ACT_OK;
}
int egress_v4_udp(struct __sk_buff *ctx) {
#if DEBUG
bpf_trace_printk("EGRESS udp4 mark %d\n", ctx->mark);
#endif
int idx = ctx->mark;
struct probe_dest *pd = destinfo.lookup(&idx);
if (pd == NULL) {
return TC_ACT_OK;
}
//
// boilerplate to get a pointer to udphdr for cloning and
// probe generation
//
int offset = NHOFFSET;
void* data = (void*)(long)ctx->data;
void* data_end = (void*)(long)ctx->data_end;
if (data + offset + sizeof(struct _iphdr) > data_end) {
return TC_ACT_OK;
}
struct _iphdr *iph = (struct _iphdr*)(data + offset);
int iphlen = (iph->verihl&0x0f) << 2;
offset = offset + iphlen;
if (data + offset + sizeof(struct _udphdr) > data_end) {
return TC_ACT_OK;
}
// decide what TTL to use in probe
u16 sport = load_half(ctx, offset + UDP_SRC_OFF);
u16 dport = load_half(ctx, offset + UDP_DST_OFF);
u64 now = bpf_ktime_get_ns();
u16 origseq = load_half(ctx, offset + UDP_CSUM_OFF);
u16 sequence = 0;
u8 newttl = 0;
u32 destaddr = load_word(ctx, NHOFFSET + IP_DST_OFF);
u16 outipid = load_half(ctx, NHOFFSET + IP_ID_OFF);
_decide_seq_ttl(pd, &sequence, &newttl);
u64 cksum64 = bpf_csum_diff(0, 0, data + NHOFFSET + IP_SRC_OFF, sizeof(u32)*2, 0);
u32 tmp = bpf_htonl(((u32)IPPROTO_UDP << 16) | 20);
cksum64 = bpf_csum_diff(0, 0, &tmp, sizeof(tmp), cksum64);
u32 srcaddr = bpf_ntohl(load_word(ctx, NHOFFSET + IP_SRC_OFF));
u32 csum = bpf_ntohl(destaddr);
csum = (csum & 0xffff) + (csum >> 16);
csum += (srcaddr & 0xffff);
csum += (srcaddr >> 16);
csum += (u16)bpf_htons(20);
csum += (u16)bpf_htons(IPPROTO_UDP);
#if DEBUG
bpf_trace_printk("EGRESS udp4 outgoing seq %lu origseq 0x%x\n", sequence, origseq);
#endif
// clone and redirect the original pkt out the intended interface
int rv = bpf_clone_redirect(ctx, IFINDEX, 0);
if (rv < 0) {
#if DEBUG
bpf_trace_printk("EGRESS udp4 bpf clone ifidx %d failed: %d\n", IFINDEX, rv);
#endif
// if clone fails, just let the packet pass w/o trying to do any modifications below
return TC_ACT_OK;
}
#ifdef DEBUG
bpf_trace_printk("EGRESS udp4 after clone emit %lu\n", sequence);
#endif
#ifdef NOTRUNCATE
#ifdef DEBUG
bpf_trace_printk("EGRESS udp4 not truncating\n");
#endif
#else
csum += bpf_htons(sport);
csum += bpf_htons(dport);
csum += bpf_htons(20);
csum += bpf_htons(sequence);
csum = (csum >> 16) + (csum & 0xffff);
csum += (csum >> 16);
csum = (u16)(~csum & 0xffff);
u16 payload[6] = {(u16)csum,0,0,0,0,0};
struct _udphdr newudp = {
.uh_sport = bpf_htons(sport),
.uh_dport = bpf_htons(dport),
.uh_ulen = bpf_htons(20),
.uh_sum = 0,
};
// get current header value
u16 curr_ip_len = load_half(ctx, NHOFFSET + IP_LEN_OFF);
u16 new_ip_len = iphlen + sizeof(struct _udphdr) + 12;
#if DEBUG
bpf_trace_printk("EGRESS udp4 truncating pkt from %d to %d\n", curr_ip_len, new_ip_len);
#endif
// truncate packet
rv = bpf_skb_change_tail(ctx, NHOFFSET + new_ip_len, 0);
if (rv < 0) {
#if DEBUG
bpf_trace_printk("EGRESS udp4 bpf trunc packet failed\n");
#endif
return TC_ACT_SHOT;
}
rv = bpf_skb_store_bytes(ctx, NHOFFSET + iphlen, &newudp, sizeof(newudp), 0);
if (rv < 0) {
#if DEBUG
bpf_trace_printk("EGRESS udp4 bpf store new udp hdr failed\n");
#endif
return TC_ACT_SHOT;
}
rv = bpf_skb_store_bytes(ctx, NHOFFSET + iphlen + sizeof(newudp), payload, sizeof(u16)*6, 0);
if (rv < 0) {
#if DEBUG
bpf_trace_printk("EGRESS udp4 bpf store payload failed\n");
#endif
return TC_ACT_SHOT;
}
rv = bpf_l4_csum_replace(ctx, NHOFFSET + iphlen + UDP_CSUM_OFF, 0, cksum64, BPF_F_PSEUDO_HDR);
if (rv < 0) {
#if DEBUG
bpf_trace_printk("EGRESS udp4 bpf store udp sum failed\n");
#endif
return TC_ACT_SHOT;
}
new_ip_len = bpf_htons(new_ip_len);
curr_ip_len = bpf_htons(curr_ip_len);
rv = bpf_l3_csum_replace(ctx, NHOFFSET + IP_CSUM_OFF, curr_ip_len, new_ip_len, 2);
if (rv < 0) {
#if DEBUG
bpf_trace_printk("EGRESS udp4 failed to replace ip csum\n");
#endif
return TC_ACT_SHOT;
}
rv = bpf_skb_store_bytes(ctx, NHOFFSET + IP_LEN_OFF, &new_ip_len, sizeof(new_ip_len), 0);
if (rv < 0) {
#if DEBUG
bpf_trace_printk("EGRESS udp4 failed to replace ip len\n");
#endif
return TC_ACT_SHOT;
}
#endif // NOTRUNCATE
u16 old_ttl_proto = load_half(ctx, NHOFFSET + IP_TTL_OFF);
u16 new_ttl_proto = bpf_htons(((u16)newttl) << 8 | IPPROTO_UDP);
rv = bpf_l3_csum_replace(ctx, NHOFFSET + IP_CSUM_OFF, bpf_htons(old_ttl_proto), new_ttl_proto, 2);
if (rv < 0) {
#if DEBUG
bpf_trace_printk("EGRESS udp4 failed to replace csum\n");
#endif
return TC_ACT_SHOT;
}
rv = bpf_skb_store_bytes(ctx, NHOFFSET + IP_TTL_OFF, &new_ttl_proto, sizeof(new_ttl_proto), 0);
if (rv < 0) {
#if DEBUG
bpf_trace_printk("EGRESS udp4 failed to store new ttl/proto\n");
#endif
return TC_ACT_SHOT;
}
rv = elf_v4_udp_beforesend(ctx);
if (rv < 0) {
#if DEBUG
bpf_trace_printk("EGRESS udp4 beforesend fail\n");
#endif
return TC_ACT_SHOT;
}
// save info about outgoing probe into hash
// hashed on: idx|sequence
u64 sentkey = (u64)idx << 32 | (u64)sequence;
_in6_addr_t destaddr6 = {{{ destaddr, 0, 0, 0 }}};
struct sent_info si = {
.send_time = now,
.dest = destaddr6,
.sport = sport,
.dport = dport,
.origseq = origseq,
.outttl = newttl,
.protocol = IPPROTO_UDP,
.outipid = outipid,
};
sentinfo.update(&sentkey, &si);
#if DEBUG
bpf_trace_printk("EGRESS udp4 emitting probe %llu\n", sequence);
#endif
return TC_ACT_OK;
}
int egress_v4(struct __sk_buff *ctx) {
int offset = NHOFFSET;
void* data = (void*)(long)ctx->data;
void* data_end = (void*)(long)ctx->data_end;
if (data + offset + sizeof(struct _iphdr) > data_end) {
return TC_ACT_OK;
}
struct _iphdr *iph = (struct _iphdr*)(data + offset);
_in6_addr_t dest = {{{ iph->daddr, 0, 0, 0 }}};
u64 *val = NULL;
if ((val = trie.lookup(&dest)) == NULL) {
return TC_ACT_OK;
}
// dest addr matches a destination of interest
int idx = (int)*val;
#ifdef DEBUG
bpf_trace_printk("EGRESS v4 dest of interest -- idx %d, currmark %d\n", idx, ctx->mark);
#endif
// store idx in ctx for later reference
ctx->mark = idx;
if (!_should_probe_dest(idx)) {
return TC_ACT_OK;
}
#ifdef DEBUG
bpf_trace_printk("EGRESS v4 should probe -- idx %d proto %d\n", idx, iph->protocol);
#endif
// jump to code to handle egress v4 for icmp/tcp/udp
egress_v4_proto.call(ctx, iph->protocol);
return TC_ACT_OK;
}
int egress_v6_icmp(struct __sk_buff *ctx) {
#if DEBUG
bpf_trace_printk("EGRESS icmp6 mark %d\n", ctx->mark);
#endif
int idx = ctx->mark;
struct probe_dest *pd = destinfo.lookup(&idx);
if (pd == NULL) {
return TC_ACT_OK;
}
//
// boilerplate to get a pointer to icmphdr for cloning and
// probe generation
//
int offset = NHOFFSET;
void* data = (void*)(long)ctx->data;
void* data_end = (void*)(long)ctx->data_end;
if (data + offset + sizeof(struct _ip6hdr) > data_end) {
return TC_ACT_OK;
}
offset = offset + sizeof(struct _ip6hdr);
struct _icmphdr *icmph = (struct _icmphdr*)(data + offset);
if (data + offset + sizeof(struct _icmphdr) > data_end) {
return TC_ACT_OK;
}
if (icmph->icmp_type != ICMP6_ECHO_REQUEST) {
return TC_ACT_OK;
}
// decide what TTL to use in probe
u64 now = bpf_ktime_get_ns();
u16 origseq = load_half(ctx, offset + ICMP_SEQ_OFF);
u16 outipid = load_half(ctx, NHOFFSET + IP6_ID_OFF);
u16 sport = load_half(ctx, offset + ICMP_TYPE_OFF);
u16 dport = load_half(ctx, offset + ICMP_CSUM_OFF);
u16 sequence = 0;
u8 newttl = 0;
_in6_addr_t destaddr;
#pragma unroll
for (int i = 0; i < 4; i++) {
destaddr._u._addr32[i] = load_word(ctx, NHOFFSET + IP6_DST_OFF + i*4);
}
_decide_seq_ttl(pd, &sequence, &newttl);
#if DEBUG
bpf_trace_printk("EGRESS icmp6 outgoing seq %lu\n", sequence);
#endif
// clone and redirect the original pkt out the intended interface
int rv = bpf_clone_redirect(ctx, IFINDEX, 0);
if (rv < 0) {
#if DEBUG
bpf_trace_printk("EGRESS icmp6 bpf clone ifidx %d failed: %d\n", IFINDEX, rv);
#endif
// if clone fails, just let the packet pass w/o trying to do any modifications below
return TC_ACT_OK;
}
#if DEBUG
bpf_trace_printk("EGRESS icmp6 after clone emit %lu\n", sequence);
#endif
if (data + offset + sizeof(struct _icmphdr) > data_end) {
return TC_ACT_SHOT;
}
// rewrite new IP ttl/hop limit
rv = bpf_skb_store_bytes(ctx, NHOFFSET + IP6_TTL_OFF, &newttl, sizeof(newttl), 0);
if (rv < 0) {
#if DEBUG
bpf_trace_printk("EGRESS icmp6 failed to store new hop limit\n");
#endif
return TC_ACT_SHOT;
}
// rewrite seq in ICMP hdr
u16 oldseq = load_half(ctx, offset + ICMP_SEQ_OFF);
u16 newseq = bpf_htons(sequence);
rv = bpf_skb_store_bytes(ctx, offset + ICMP_SEQ_OFF, &newseq, sizeof(newseq), 0);
if (rv < 0) {
#if DEBUG
bpf_trace_printk("EGRESS icmp6 failed to store new icmp seq\n");
#endif
return TC_ACT_SHOT;
}
newseq = bpf_ntohs(newseq);
// fixup ICMP checksum
u16 oldcsum = load_half(ctx, offset + ICMP_CSUM_OFF);
u16 newcsum = oldcsum - (newseq - oldseq);
newcsum = bpf_htons(newcsum);
rv = bpf_skb_store_bytes(ctx, offset + ICMP_CSUM_OFF, &newcsum, sizeof(newcsum), 0);
if (rv < 0) {
#if DEBUG
bpf_trace_printk("EGRESS icmp6 failed to store new icmp csum\n");
#endif
return TC_ACT_SHOT;
}
rv = elf_v6_icmp_beforesend(ctx);
if (rv < 0) {
#if DEBUG
bpf_trace_printk("EGRESS icmp6 beforesend fail\n");
#endif
return TC_ACT_SHOT;
}
// save info about outgoing probe into hash
// hashed on: idx|sequence
u64 sentkey = (u64)idx << 32 | (u64)sequence;
struct sent_info si = {
.send_time = now,
.dest = destaddr,
.sport = sport,
.dport = dport,
.origseq = origseq,
.outttl = newttl,
.protocol = IPPROTO_ICMP6,
.outipid = outipid,
};
sentinfo.update(&sentkey, &si);
#if DEBUG
bpf_trace_printk("EGRESS icmp6 emitting probe %lu\n", sequence);
#endif
return TC_ACT_OK;
}
int egress_v6_tcp(struct __sk_buff *ctx) {
#if DEBUG
bpf_trace_printk("EGRESS tcp6 mark %d\n", ctx->mark);
#endif
int idx = ctx->mark;
struct probe_dest *pd = destinfo.lookup(&idx);
if (pd == NULL) {
return TC_ACT_OK;
}
//
// boilerplate to get a pointer to tcphdr for cloning and
// probe generation
//
int offset = NHOFFSET;
void* data = (void*)(long)ctx->data;
void* data_end = (void*)(long)ctx->data_end;
if (data + offset + sizeof(struct _ip6hdr) > data_end) {
return TC_ACT_OK;
}
offset = offset + sizeof(struct _ip6hdr);
if (data + offset + sizeof(struct _tcphdr) > data_end) {
return TC_ACT_OK;
}
// decide what TTL to use in probe
u16 sport = load_half(ctx, offset + TCP_SRC_OFF);
u16 dport = load_half(ctx, offset + TCP_DST_OFF);
u64 now = bpf_ktime_get_ns();
u32 origseq = bpf_ntohs(load_word(ctx, offset + TCP_SEQ_OFF));
u16 sequence = 0;
u8 newttl = 0;
_in6_addr_t destaddr;
// compute checksum pseudoheader value
u64 cksum64 = bpf_csum_diff(0, 0, data + NHOFFSET + IP6_SRC_OFF, sizeof(_in6_addr_t) * 2, 0);
u32 tmp = bpf_htonl(20); // tcp hdr size
cksum64 = bpf_csum_diff(0, 0, &tmp, sizeof(tmp), cksum64);
tmp = bpf_htonl(IPPROTO_TCP);
cksum64 = bpf_csum_diff(0, 0, &tmp, sizeof(tmp), cksum64);
#pragma unroll
for (int i = 0; i < 4; i++) {
destaddr._u._addr32[i] = load_word(ctx, NHOFFSET + IP6_DST_OFF + i*4);
}
u16 outipid = load_half(ctx, NHOFFSET + IP6_ID_OFF);