hc
2023-12-06 d38611ca164021d018c1b23eee65bbebc09c63e0
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
// SPDX-License-Identifier: GPL-2.0
#include <string.h>
#include <stdbool.h>
 
#include <linux/bpf.h>
#include <linux/in.h>
#include <linux/in6.h>
#include <sys/socket.h>
 
#include <bpf/bpf_helpers.h>
#include <bpf/bpf_endian.h>
 
char _license[] SEC("license") = "GPL";
int _version SEC("version") = 1;
 
struct svc_addr {
   __be32 addr;
   __be16 port;
};
 
struct {
   __uint(type, BPF_MAP_TYPE_SK_STORAGE);
   __uint(map_flags, BPF_F_NO_PREALLOC);
   __type(key, int);
   __type(value, struct svc_addr);
} service_mapping SEC(".maps");
 
SEC("cgroup/connect4")
int connect4(struct bpf_sock_addr *ctx)
{
   struct sockaddr_in sa = {};
   struct svc_addr *orig;
 
   /* Force local address to 127.0.0.1:22222. */
   sa.sin_family = AF_INET;
   sa.sin_port = bpf_htons(22222);
   sa.sin_addr.s_addr = bpf_htonl(0x7f000001);
 
   if (bpf_bind(ctx, (struct sockaddr *)&sa, sizeof(sa)) != 0)
       return 0;
 
   /* Rewire service 1.2.3.4:60000 to backend 127.0.0.1:60123. */
   if (ctx->user_port == bpf_htons(60000)) {
       orig = bpf_sk_storage_get(&service_mapping, ctx->sk, 0,
                     BPF_SK_STORAGE_GET_F_CREATE);
       if (!orig)
           return 0;
 
       orig->addr = ctx->user_ip4;
       orig->port = ctx->user_port;
 
       ctx->user_ip4 = bpf_htonl(0x7f000001);
       ctx->user_port = bpf_htons(60123);
   }
   return 1;
}
 
SEC("cgroup/getsockname4")
int getsockname4(struct bpf_sock_addr *ctx)
{
   /* Expose local server as 1.2.3.4:60000 to client. */
   if (ctx->user_port == bpf_htons(60123)) {
       ctx->user_ip4 = bpf_htonl(0x01020304);
       ctx->user_port = bpf_htons(60000);
   }
   return 1;
}
 
SEC("cgroup/getpeername4")
int getpeername4(struct bpf_sock_addr *ctx)
{
   struct svc_addr *orig;
 
   /* Expose service 1.2.3.4:60000 as peer instead of backend. */
   if (ctx->user_port == bpf_htons(60123)) {
       orig = bpf_sk_storage_get(&service_mapping, ctx->sk, 0, 0);
       if (orig) {
           ctx->user_ip4 = orig->addr;
           ctx->user_port = orig->port;
       }
   }
   return 1;
}