forked from YSRossi/ebpf-tcp-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbpf_sockops.c
41 lines (34 loc) · 969 Bytes
/
bpf_sockops.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
#include "bpf_sockops.h"
static inline void update_sockmap_ops(struct bpf_sock_ops *skops)
{
struct sockmap_key skm_key = {
.family = skops->family,
.remote_ip4 = skops->remote_ip4,
.local_ip4 = skops->local_ip4,
.remote_port = bpf_ntohl(skops->remote_port),
.local_port = skops->local_port,
};
int ret;
ret = bpf_sock_hash_update(skops, &sockmap_ops, &skm_key, BPF_NOEXIST);
if (ret) {
bpf_printk("Update map failed. %d\n", -ret);
return;
}
}
SEC("sockops")
int bpf_sockmap(struct bpf_sock_ops *skops)
{
/* Only support IPv4 */
if (skops->family != AF_INET)
return 0;
switch (skops->op) {
case BPF_SOCK_OPS_PASSIVE_ESTABLISHED_CB:
case BPF_SOCK_OPS_ACTIVE_ESTABLISHED_CB:
update_sockmap_ops(skops);
break;
default:
break;
}
return 0;
}
SEC("license") const char __license[] = "GPL";