1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (c) 2018 Facebook
4 #include <linux/stddef.h>
6 #include <sys/socket.h>
8 #include <bpf/bpf_helpers.h>
9 #include <bpf/bpf_endian.h>
11 #include <bpf_sockopt_helpers.h>
13 #define SRC_REWRITE_IP6_0 0
14 #define SRC_REWRITE_IP6_1 0
15 #define SRC_REWRITE_IP6_2 0
16 #define SRC_REWRITE_IP6_3 6
18 #define DST_REWRITE_IP6_0 0
19 #define DST_REWRITE_IP6_1 0
20 #define DST_REWRITE_IP6_2 0
21 #define DST_REWRITE_IP6_3 1
23 #define DST_REWRITE_IP6_V4_MAPPED_0 0
24 #define DST_REWRITE_IP6_V4_MAPPED_1 0
25 #define DST_REWRITE_IP6_V4_MAPPED_2 0x0000FFFF
26 #define DST_REWRITE_IP6_V4_MAPPED_3 0xc0a80004 // 192.168.0.4
28 #define DST_REWRITE_PORT6 6666
30 SEC("cgroup/sendmsg6")
31 int sendmsg_v6_prog(struct bpf_sock_addr *ctx)
33 if (ctx->type != SOCK_DGRAM)
36 if (!get_set_sk_priority(ctx))
40 if (ctx->msg_src_ip6[3] == bpf_htonl(1) ||
41 ctx->msg_src_ip6[3] == bpf_htonl(0)) {
42 ctx->msg_src_ip6[0] = bpf_htonl(SRC_REWRITE_IP6_0);
43 ctx->msg_src_ip6[1] = bpf_htonl(SRC_REWRITE_IP6_1);
44 ctx->msg_src_ip6[2] = bpf_htonl(SRC_REWRITE_IP6_2);
45 ctx->msg_src_ip6[3] = bpf_htonl(SRC_REWRITE_IP6_3);
47 /* Unexpected source. Reject sendmsg. */
51 /* Rewrite destination. */
52 if (ctx->user_ip6[0] == bpf_htonl(0xFACEB00C)) {
53 ctx->user_ip6[0] = bpf_htonl(DST_REWRITE_IP6_0);
54 ctx->user_ip6[1] = bpf_htonl(DST_REWRITE_IP6_1);
55 ctx->user_ip6[2] = bpf_htonl(DST_REWRITE_IP6_2);
56 ctx->user_ip6[3] = bpf_htonl(DST_REWRITE_IP6_3);
58 ctx->user_port = bpf_htons(DST_REWRITE_PORT6);
60 /* Unexpected destination. Reject sendmsg. */
67 SEC("cgroup/sendmsg6")
68 int sendmsg_v6_v4mapped_prog(struct bpf_sock_addr *ctx)
71 ctx->msg_src_ip6[0] = bpf_htonl(SRC_REWRITE_IP6_0);
72 ctx->msg_src_ip6[1] = bpf_htonl(SRC_REWRITE_IP6_1);
73 ctx->msg_src_ip6[2] = bpf_htonl(SRC_REWRITE_IP6_2);
74 ctx->msg_src_ip6[3] = bpf_htonl(SRC_REWRITE_IP6_3);
76 /* Rewrite destination. */
77 ctx->user_ip6[0] = bpf_htonl(DST_REWRITE_IP6_V4_MAPPED_0);
78 ctx->user_ip6[1] = bpf_htonl(DST_REWRITE_IP6_V4_MAPPED_1);
79 ctx->user_ip6[2] = bpf_htonl(DST_REWRITE_IP6_V4_MAPPED_2);
80 ctx->user_ip6[3] = bpf_htonl(DST_REWRITE_IP6_V4_MAPPED_3);
82 ctx->user_port = bpf_htons(DST_REWRITE_PORT6);
87 SEC("cgroup/sendmsg6")
88 int sendmsg_v6_wildcard_prog(struct bpf_sock_addr *ctx)
91 ctx->msg_src_ip6[0] = bpf_htonl(SRC_REWRITE_IP6_0);
92 ctx->msg_src_ip6[1] = bpf_htonl(SRC_REWRITE_IP6_1);
93 ctx->msg_src_ip6[2] = bpf_htonl(SRC_REWRITE_IP6_2);
94 ctx->msg_src_ip6[3] = bpf_htonl(SRC_REWRITE_IP6_3);
96 /* Rewrite destination. */
97 ctx->user_ip6[0] = bpf_htonl(0);
98 ctx->user_ip6[1] = bpf_htonl(0);
99 ctx->user_ip6[2] = bpf_htonl(0);
100 ctx->user_ip6[3] = bpf_htonl(0);
102 ctx->user_port = bpf_htons(DST_REWRITE_PORT6);
107 SEC("cgroup/sendmsg6")
108 int sendmsg_v6_preserve_dst_prog(struct bpf_sock_addr *ctx)
113 SEC("cgroup/sendmsg6")
114 int sendmsg_v6_deny_prog(struct bpf_sock_addr *ctx)
119 char _license[] SEC("license") = "GPL";