-
Notifications
You must be signed in to change notification settings - Fork 2
/
bpf_redir.c
45 lines (37 loc) · 1.25 KB
/
bpf_redir.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
#include "bpf_sockops.h"
#define SERVER_PORT 12345
SEC("sk_skb/stream_verdict")
int bpf_redir(struct __sk_buff * skb)
{
if (skb->family != AF_INET)
return SK_PASS;
if (skb->local_port != SERVER_PORT)
return SK_PASS;
if (skb->len == 0)
return SK_PASS;
int ret;
if (skb->remote_ip4 == skb->local_ip4) {
struct sockmap_key skm_key = {
.family = skb->family,
.remote_ip4 = skb->remote_ip4,
.local_ip4 = skb->local_ip4,
.remote_port = skb->local_port,
.local_port = bpf_ntohl(skb->remote_port),
};
ret = bpf_sk_redirect_hash(skb, &sockmap_ops, &skm_key,
BPF_F_INGRESS);
} else {
struct sockmap_key skm_key = {
.family = skb->family,
.remote_ip4 = skb->remote_ip4,
.local_ip4 = skb->local_ip4,
.remote_port = bpf_ntohl(skb->remote_port),
.local_port = skb->local_port,
};
ret = bpf_sk_redirect_hash(skb, &sockmap_ops, &skm_key, 0);
}
if (ret != SK_PASS)
bpf_printk("bpf_sk_redirect_hash() failed %d, error \n", -ret);
return ret;
}
char _license[] SEC("license") = "GPL";