-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathutils.h
290 lines (257 loc) · 10 KB
/
utils.h
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
#ifndef __UTILS_H__
#define __UTILS_H__
#include <bpf_core_read.h>
#include "types.h"
static u8 do_sampling = 0;
// sets the TCP header flags for connection information
static inline void set_flags(struct tcphdr *th, u16 *flags) {
//If both ACK and SYN are set, then it is server -> client communication during 3-way handshake.
if (th->ack && th->syn) {
*flags |= SYN_ACK_FLAG;
} else if (th->ack && th->fin) {
// If both ACK and FIN are set, then it is graceful termination from server.
*flags |= FIN_ACK_FLAG;
} else if (th->ack && th->rst) {
// If both ACK and RST are set, then it is abrupt connection termination.
*flags |= RST_ACK_FLAG;
} else if (th->fin) {
*flags |= FIN_FLAG;
} else if (th->syn) {
*flags |= SYN_FLAG;
} else if (th->ack) {
*flags |= ACK_FLAG;
} else if (th->rst) {
*flags |= RST_FLAG;
} else if (th->psh) {
*flags |= PSH_FLAG;
} else if (th->urg) {
*flags |= URG_FLAG;
} else if (th->ece) {
*flags |= ECE_FLAG;
} else if (th->cwr) {
*flags |= CWR_FLAG;
}
}
// Extract L4 info for the supported protocols
static inline void fill_l4info(void *l4_hdr_start, void *data_end, u8 protocol, pkt_info *pkt) {
flow_id *id = pkt->id;
id->transport_protocol = protocol;
switch (protocol) {
case IPPROTO_TCP: {
struct tcphdr *tcp = l4_hdr_start;
if ((void *)tcp + sizeof(*tcp) <= data_end) {
id->src_port = bpf_ntohs(tcp->source);
id->dst_port = bpf_ntohs(tcp->dest);
set_flags(tcp, &pkt->flags);
pkt->l4_hdr = (void *)tcp;
}
} break;
case IPPROTO_UDP: {
struct udphdr *udp = l4_hdr_start;
if ((void *)udp + sizeof(*udp) <= data_end) {
id->src_port = bpf_ntohs(udp->source);
id->dst_port = bpf_ntohs(udp->dest);
pkt->l4_hdr = (void *)udp;
}
} break;
case IPPROTO_SCTP: {
struct sctphdr *sctph = l4_hdr_start;
if ((void *)sctph + sizeof(*sctph) <= data_end) {
id->src_port = bpf_ntohs(sctph->source);
id->dst_port = bpf_ntohs(sctph->dest);
pkt->l4_hdr = (void *)sctph;
}
} break;
case IPPROTO_ICMP: {
struct icmphdr *icmph = l4_hdr_start;
if ((void *)icmph + sizeof(*icmph) <= data_end) {
id->icmp_type = icmph->type;
id->icmp_code = icmph->code;
pkt->l4_hdr = (void *)icmph;
}
} break;
case IPPROTO_ICMPV6: {
struct icmp6hdr *icmp6h = l4_hdr_start;
if ((void *)icmp6h + sizeof(*icmp6h) <= data_end) {
id->icmp_type = icmp6h->icmp6_type;
id->icmp_code = icmp6h->icmp6_code;
pkt->l4_hdr = (void *)icmp6h;
}
} break;
default:
break;
}
}
static inline u8 ipv4_get_dscp(const struct iphdr *iph) {
return (iph->tos >> DSCP_SHIFT) & DSCP_MASK;
}
static inline u8 ipv6_get_dscp(const struct ipv6hdr *ipv6h) {
return ((bpf_ntohs(*(const __be16 *)ipv6h) >> 4) >> DSCP_SHIFT) & DSCP_MASK;
}
// sets flow fields from IPv4 header information
static inline int fill_iphdr(struct iphdr *ip, void *data_end, pkt_info *pkt) {
void *l4_hdr_start;
l4_hdr_start = (void *)ip + sizeof(*ip);
if (l4_hdr_start > data_end) {
return DISCARD;
}
flow_id *id = pkt->id;
/* Save the IP Address to id directly. copy once. */
__builtin_memcpy(id->src_ip, ip4in6, sizeof(ip4in6));
__builtin_memcpy(id->dst_ip, ip4in6, sizeof(ip4in6));
__builtin_memcpy(id->src_ip + sizeof(ip4in6), &ip->saddr, sizeof(ip->saddr));
__builtin_memcpy(id->dst_ip + sizeof(ip4in6), &ip->daddr, sizeof(ip->daddr));
pkt->dscp = ipv4_get_dscp(ip);
/* fill l4 header which will be added to id in flow_monitor function.*/
fill_l4info(l4_hdr_start, data_end, ip->protocol, pkt);
return SUBMIT;
}
// sets flow fields from IPv6 header information
static inline int fill_ip6hdr(struct ipv6hdr *ip, void *data_end, pkt_info *pkt) {
void *l4_hdr_start;
l4_hdr_start = (void *)ip + sizeof(*ip);
if (l4_hdr_start > data_end) {
return DISCARD;
}
flow_id *id = pkt->id;
/* Save the IP Address to id directly. copy once. */
__builtin_memcpy(id->src_ip, ip->saddr.in6_u.u6_addr8, IP_MAX_LEN);
__builtin_memcpy(id->dst_ip, ip->daddr.in6_u.u6_addr8, IP_MAX_LEN);
pkt->dscp = ipv6_get_dscp(ip);
/* fill l4 header which will be added to id in flow_monitor function.*/
fill_l4info(l4_hdr_start, data_end, ip->nexthdr, pkt);
return SUBMIT;
}
// sets flow fields from Ethernet header information
static inline int fill_ethhdr(struct ethhdr *eth, void *data_end, pkt_info *pkt,
u16 *eth_protocol) {
if ((void *)eth + sizeof(*eth) > data_end) {
return DISCARD;
}
flow_id *id = pkt->id;
*eth_protocol = bpf_ntohs(eth->h_proto);
if (*eth_protocol == ETH_P_IP) {
struct iphdr *ip = (void *)eth + sizeof(*eth);
return fill_iphdr(ip, data_end, pkt);
} else if (*eth_protocol == ETH_P_IPV6) {
struct ipv6hdr *ip6 = (void *)eth + sizeof(*eth);
return fill_ip6hdr(ip6, data_end, pkt);
} else {
// TODO : Need to implement other specific ethertypes if needed
// For now other parts of flow id remain zero
__builtin_memset(&(id->src_ip), 0, sizeof(struct in6_addr));
__builtin_memset(&(id->dst_ip), 0, sizeof(struct in6_addr));
id->transport_protocol = 0;
id->src_port = 0;
id->dst_port = 0;
}
return SUBMIT;
}
static inline void core_fill_in_l2(struct sk_buff *skb, u16 *eth_protocol, u16 *family) {
struct ethhdr eth;
__builtin_memset(ð, 0, sizeof(eth));
u8 *skb_head = BPF_CORE_READ(skb, head);
u16 skb_mac_header = BPF_CORE_READ(skb, mac_header);
bpf_probe_read(ð, sizeof(eth), (struct ethhdr *)(skb_head + skb_mac_header));
*eth_protocol = bpf_ntohs(eth.h_proto);
if (*eth_protocol == ETH_P_IP) {
*family = AF_INET;
} else if (*eth_protocol == ETH_P_IPV6) {
*family = AF_INET6;
}
}
static inline void core_fill_in_l3(struct sk_buff *skb, flow_id *id, u16 family, u8 *protocol,
u8 *dscp) {
u16 skb_network_header = BPF_CORE_READ(skb, network_header);
u8 *skb_head = BPF_CORE_READ(skb, head);
switch (family) {
case AF_INET: {
struct iphdr ip;
__builtin_memset(&ip, 0, sizeof(ip));
bpf_probe_read(&ip, sizeof(ip), (struct iphdr *)(skb_head + skb_network_header));
__builtin_memcpy(id->src_ip, ip4in6, sizeof(ip4in6));
__builtin_memcpy(id->dst_ip, ip4in6, sizeof(ip4in6));
__builtin_memcpy(id->src_ip + sizeof(ip4in6), &ip.saddr, sizeof(ip.saddr));
__builtin_memcpy(id->dst_ip + sizeof(ip4in6), &ip.daddr, sizeof(ip.daddr));
*dscp = ipv4_get_dscp(&ip);
*protocol = ip.protocol;
break;
}
case AF_INET6: {
struct ipv6hdr ip;
__builtin_memset(&ip, 0, sizeof(ip));
bpf_probe_read(&ip, sizeof(ip), (struct ipv6hdr *)(skb_head + skb_network_header));
__builtin_memcpy(id->src_ip, ip.saddr.in6_u.u6_addr8, IP_MAX_LEN);
__builtin_memcpy(id->dst_ip, ip.daddr.in6_u.u6_addr8, IP_MAX_LEN);
*dscp = ipv6_get_dscp(&ip);
*protocol = ip.nexthdr;
break;
}
default:
return;
}
}
static inline void core_fill_in_tcp(struct sk_buff *skb, flow_id *id, u16 *flags) {
u16 skb_transport_header = BPF_CORE_READ(skb, transport_header);
u8 *skb_head = BPF_CORE_READ(skb, head);
struct tcphdr tcp;
u16 sport, dport;
__builtin_memset(&tcp, 0, sizeof(tcp));
bpf_probe_read(&tcp, sizeof(tcp), (struct tcphdr *)(skb_head + skb_transport_header));
sport = bpf_ntohs(tcp.source);
dport = bpf_ntohs(tcp.dest);
id->src_port = sport;
id->dst_port = dport;
set_flags(&tcp, flags);
id->transport_protocol = IPPROTO_TCP;
}
static inline void core_fill_in_udp(struct sk_buff *skb, flow_id *id) {
u16 skb_transport_header = BPF_CORE_READ(skb, transport_header);
u8 *skb_head = BPF_CORE_READ(skb, head);
struct udphdr udp;
u16 sport, dport;
__builtin_memset(&udp, 0, sizeof(udp));
bpf_probe_read(&udp, sizeof(udp), (struct udphdr *)(skb_head + skb_transport_header));
sport = bpf_ntohs(udp.source);
dport = bpf_ntohs(udp.dest);
id->src_port = sport;
id->dst_port = dport;
id->transport_protocol = IPPROTO_UDP;
}
static inline void core_fill_in_sctp(struct sk_buff *skb, flow_id *id) {
u16 skb_transport_header = BPF_CORE_READ(skb, transport_header);
u8 *skb_head = BPF_CORE_READ(skb, head);
struct sctphdr sctp;
u16 sport, dport;
__builtin_memset(&sctp, 0, sizeof(sctp));
bpf_probe_read(&sctp, sizeof(sctp), (struct sctphdr *)(skb_head + skb_transport_header));
sport = bpf_ntohs(sctp.source);
dport = bpf_ntohs(sctp.dest);
id->src_port = sport;
id->dst_port = dport;
id->transport_protocol = IPPROTO_SCTP;
}
static inline void core_fill_in_icmpv4(struct sk_buff *skb, flow_id *id) {
u16 skb_transport_header = BPF_CORE_READ(skb, transport_header);
u8 *skb_head = BPF_CORE_READ(skb, head);
struct icmphdr icmph;
__builtin_memset(&icmph, 0, sizeof(icmph));
bpf_probe_read(&icmph, sizeof(icmph), (struct icmphdr *)(skb_head + skb_transport_header));
id->icmp_type = icmph.type;
id->icmp_code = icmph.code;
id->transport_protocol = IPPROTO_ICMP;
}
static inline void core_fill_in_icmpv6(struct sk_buff *skb, flow_id *id) {
u16 skb_transport_header = BPF_CORE_READ(skb, transport_header);
u8 *skb_head = BPF_CORE_READ(skb, head);
struct icmp6hdr icmph;
__builtin_memset(&icmph, 0, sizeof(icmph));
bpf_probe_read(&icmph, sizeof(icmph), (struct icmp6hdr *)(skb_head + skb_transport_header));
id->icmp_type = icmph.icmp6_type;
id->icmp_code = icmph.icmp6_code;
id->transport_protocol = IPPROTO_ICMPV6;
}
static inline void fill_in_others_protocol(flow_id *id, u8 protocol) {
id->transport_protocol = protocol;
}
#endif // __UTILS_H__