Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 #include <string.h>
0003 
0004 #include <linux/bpf.h>
0005 #include <linux/in.h>
0006 #include <linux/in6.h>
0007 #include <sys/socket.h>
0008 
0009 #include <bpf/bpf_helpers.h>
0010 #include <bpf/bpf_endian.h>
0011 
0012 #include <bpf_sockopt_helpers.h>
0013 
0014 char _license[] SEC("license") = "GPL";
0015 
0016 struct svc_addr {
0017     __be32 addr[4];
0018     __be16 port;
0019 };
0020 
0021 struct {
0022     __uint(type, BPF_MAP_TYPE_SK_STORAGE);
0023     __uint(map_flags, BPF_F_NO_PREALLOC);
0024     __type(key, int);
0025     __type(value, struct svc_addr);
0026 } service_mapping SEC(".maps");
0027 
0028 SEC("cgroup/connect6")
0029 int connect6(struct bpf_sock_addr *ctx)
0030 {
0031     struct sockaddr_in6 sa = {};
0032     struct svc_addr *orig;
0033 
0034     /* Force local address to [::1]:22223. */
0035     sa.sin6_family = AF_INET6;
0036     sa.sin6_port = bpf_htons(22223);
0037     sa.sin6_addr.s6_addr32[3] = bpf_htonl(1);
0038 
0039     if (bpf_bind(ctx, (struct sockaddr *)&sa, sizeof(sa)) != 0)
0040         return 0;
0041 
0042     /* Rewire service [fc00::1]:60000 to backend [::1]:60124. */
0043     if (ctx->user_port == bpf_htons(60000)) {
0044         orig = bpf_sk_storage_get(&service_mapping, ctx->sk, 0,
0045                       BPF_SK_STORAGE_GET_F_CREATE);
0046         if (!orig)
0047             return 0;
0048 
0049         orig->addr[0] = ctx->user_ip6[0];
0050         orig->addr[1] = ctx->user_ip6[1];
0051         orig->addr[2] = ctx->user_ip6[2];
0052         orig->addr[3] = ctx->user_ip6[3];
0053         orig->port = ctx->user_port;
0054 
0055         ctx->user_ip6[0] = 0;
0056         ctx->user_ip6[1] = 0;
0057         ctx->user_ip6[2] = 0;
0058         ctx->user_ip6[3] = bpf_htonl(1);
0059         ctx->user_port = bpf_htons(60124);
0060     }
0061     return 1;
0062 }
0063 
0064 SEC("cgroup/getsockname6")
0065 int getsockname6(struct bpf_sock_addr *ctx)
0066 {
0067     if (!get_set_sk_priority(ctx))
0068         return 1;
0069 
0070     /* Expose local server as [fc00::1]:60000 to client. */
0071     if (ctx->user_port == bpf_htons(60124)) {
0072         ctx->user_ip6[0] = bpf_htonl(0xfc000000);
0073         ctx->user_ip6[1] = 0;
0074         ctx->user_ip6[2] = 0;
0075         ctx->user_ip6[3] = bpf_htonl(1);
0076         ctx->user_port = bpf_htons(60000);
0077     }
0078     return 1;
0079 }
0080 
0081 SEC("cgroup/getpeername6")
0082 int getpeername6(struct bpf_sock_addr *ctx)
0083 {
0084     struct svc_addr *orig;
0085 
0086     if (!get_set_sk_priority(ctx))
0087         return 1;
0088 
0089     /* Expose service [fc00::1]:60000 as peer instead of backend. */
0090     if (ctx->user_port == bpf_htons(60124)) {
0091         orig = bpf_sk_storage_get(&service_mapping, ctx->sk, 0, 0);
0092         if (orig) {
0093             ctx->user_ip6[0] = orig->addr[0];
0094             ctx->user_ip6[1] = orig->addr[1];
0095             ctx->user_ip6[2] = orig->addr[2];
0096             ctx->user_ip6[3] = orig->addr[3];
0097             ctx->user_port = orig->port;
0098         }
0099     }
0100     return 1;
0101 }