-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathtraceroute.c
168 lines (131 loc) · 4.59 KB
/
traceroute.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
//go:build ignore
// SPDX-License-Identifier: GPL-2.0
#include "bpf_all.h"
#define ICMP_TIME_EXCEEDED 11 /* Time Exceeded */
#define ICMP_EXC_TTL 0 /* TTL count exceeded */
static volatile const __u32 MY_ADDR = 0;
static __always_inline void
__update_icmp_checksum(struct icmphdr *icmph, int size)
{
icmph->checksum = 0;
icmph->checksum = ipv4_csum(icmph, size);
}
static __always_inline void
__update_ip_checksum(struct iphdr *iph)
{
iph->check = 0;
iph->check = ipv4_csum(iph, sizeof(*iph));
}
static __always_inline int
__trim_payload(struct xdp_md *ctx, struct ethhdr *eth, struct iphdr *iph,
__u64 *icmp_payload)
{
int pkt_len = ctx->data_end - ctx->data, trim_size;
int payload_len, iph_len = sizeof(*iph);
struct icmphdr *icmph;
struct tcphdr *tcph;
struct udphdr *udph;
bool move_hdr = iph->ihl != 5;
switch (iph->protocol) {
case IPPROTO_TCP:
payload_len = iph_len + sizeof(struct tcphdr);
if (move_hdr) {
tcph = (struct tcphdr *)((void *) iph + (iph->ihl << 2));
if ((void *)(__u64) (tcph + 1) > ctx_ptr(ctx, data_end))
return XDP_PASS;
if ((void *)(__u64) (iph+1) + sizeof(*tcph) > ctx_ptr(ctx, data_end))
return XDP_PASS;
__builtin_memcpy(iph+1, tcph, sizeof(*tcph));
}
break;
case IPPROTO_UDP:
payload_len = iph_len + sizeof(struct udphdr);
if (move_hdr) {
udph = (struct udphdr *)((void *) iph + (iph->ihl << 2));
if ((void *)(__u64) (udph + 1) > ctx_ptr(ctx, data_end))
return XDP_PASS;
if ((void *)(__u64) (iph+1) + sizeof(*udph) > ctx_ptr(ctx, data_end))
return XDP_PASS;
__builtin_memcpy(iph+1, udph, sizeof(*udph));
}
break;
case IPPROTO_ICMP:
payload_len = iph_len + sizeof(struct icmphdr);
if (move_hdr) {
icmph = (struct icmphdr *)((void *) iph + (iph->ihl << 2));
if ((void *)(__u64) (icmph + 1) > ctx_ptr(ctx, data_end))
return XDP_PASS;
if ((void *)(__u64) (iph+1) + sizeof(*icmph) > ctx_ptr(ctx, data_end))
return XDP_PASS;
__builtin_memcpy(iph+1, icmph, sizeof(*icmph));
}
break;
default:
return XDP_PASS;
}
*icmp_payload = payload_len;
trim_size = pkt_len - sizeof(*eth) - payload_len;
if (trim_size < 0)
return XDP_PASS;
if (trim_size > 0 && bpf_xdp_adjust_tail(ctx, -trim_size))
return XDP_PASS;
return 0;
}
static __always_inline int
__expand_icmp_headroom(struct xdp_md *ctx)
{
const int siz = (sizeof(struct iphdr) + sizeof(struct icmphdr));
return bpf_xdp_adjust_head(ctx, -siz);
}
static __always_inline int
__encode_icmp_packet(struct xdp_md *ctx, struct ethhdr *org_eth,
__u64 icmp_payload, __u32 sip, __u16 id)
{
struct ethhdr *eth = (struct ethhdr *) ctx_ptr(ctx, data);
struct iphdr *iph = (struct iphdr *)(eth + 1);
struct icmphdr *icmph = (struct icmphdr *)(iph + 1);
if ((void *)(__u64) (icmph + 1) + icmp_payload > ctx_ptr(ctx, data_end))
return XDP_PASS;
__builtin_memcpy(eth->h_dest, org_eth->h_source, ETH_ALEN);
__builtin_memcpy(eth->h_source, org_eth->h_dest, ETH_ALEN);
eth->h_proto = bpf_htons(ETH_P_IP);
iph->version = 4;
iph->ihl = sizeof(*iph) >> 2;
iph->tos = 0x2b; // Custom TOS to identify the packet.
iph->tot_len = bpf_htons(sizeof(*iph) + sizeof(*icmph) + icmp_payload);
iph->id = id;
iph->frag_off = 0;
iph->ttl = 64;
iph->protocol = IPPROTO_ICMP;
iph->saddr = MY_ADDR;
iph->daddr = sip;
__update_ip_checksum(iph);
icmph->type = ICMP_TIME_EXCEEDED;
icmph->code = ICMP_EXC_TTL;
icmph->un.gateway = 0;
__update_icmp_checksum(icmph, sizeof(*icmph) + icmp_payload);
return XDP_TX;
}
SEC("xdp")
int traceroute(struct xdp_md *ctx)
{
struct ethhdr *eth = (struct ethhdr *) ctx_ptr(ctx, data), copied;
struct iphdr *iph = (struct iphdr *)(eth + 1);
__u64 icmp_payload;
__u32 sip;
__u16 id;
if ((void *)(__u64) (iph + 1) > ctx_ptr(ctx, data_end))
return XDP_PASS;
if (eth->h_proto != bpf_htons(ETH_P_IP))
return XDP_PASS;
if (iph->ttl > 1)
return XDP_PASS;
sip = iph->saddr;
id = iph->id;
__builtin_memcpy(&copied, eth, sizeof(copied));
if (__trim_payload(ctx, eth, iph, &icmp_payload))
return XDP_PASS;
if (__expand_icmp_headroom(ctx))
return XDP_PASS;
return __encode_icmp_packet(ctx, &copied, icmp_payload, sip, id);
}