Loading...
1// SPDX-License-Identifier: GPL-2.0
2// Copyright (c) 2018 Facebook
3
4#include <linux/stddef.h>
5#include <linux/bpf.h>
6#include <sys/socket.h>
7
8#include <bpf/bpf_helpers.h>
9#include <bpf/bpf_endian.h>
10
11#define SRC_REWRITE_IP6_0 0
12#define SRC_REWRITE_IP6_1 0
13#define SRC_REWRITE_IP6_2 0
14#define SRC_REWRITE_IP6_3 6
15
16#define DST_REWRITE_IP6_0 0
17#define DST_REWRITE_IP6_1 0
18#define DST_REWRITE_IP6_2 0
19#define DST_REWRITE_IP6_3 1
20
21#define DST_REWRITE_PORT6 6666
22
23int _version SEC("version") = 1;
24
25SEC("cgroup/sendmsg6")
26int sendmsg_v6_prog(struct bpf_sock_addr *ctx)
27{
28 if (ctx->type != SOCK_DGRAM)
29 return 0;
30
31 /* Rewrite source. */
32 if (ctx->msg_src_ip6[3] == bpf_htonl(1) ||
33 ctx->msg_src_ip6[3] == bpf_htonl(0)) {
34 ctx->msg_src_ip6[0] = bpf_htonl(SRC_REWRITE_IP6_0);
35 ctx->msg_src_ip6[1] = bpf_htonl(SRC_REWRITE_IP6_1);
36 ctx->msg_src_ip6[2] = bpf_htonl(SRC_REWRITE_IP6_2);
37 ctx->msg_src_ip6[3] = bpf_htonl(SRC_REWRITE_IP6_3);
38 } else {
39 /* Unexpected source. Reject sendmsg. */
40 return 0;
41 }
42
43 /* Rewrite destination. */
44 if (ctx->user_ip6[0] == bpf_htonl(0xFACEB00C)) {
45 ctx->user_ip6[0] = bpf_htonl(DST_REWRITE_IP6_0);
46 ctx->user_ip6[1] = bpf_htonl(DST_REWRITE_IP6_1);
47 ctx->user_ip6[2] = bpf_htonl(DST_REWRITE_IP6_2);
48 ctx->user_ip6[3] = bpf_htonl(DST_REWRITE_IP6_3);
49
50 ctx->user_port = bpf_htons(DST_REWRITE_PORT6);
51 } else {
52 /* Unexpected destination. Reject sendmsg. */
53 return 0;
54 }
55
56 return 1;
57}
58
59char _license[] SEC("license") = "GPL";
1// SPDX-License-Identifier: GPL-2.0
2// Copyright (c) 2018 Facebook
3
4#include <linux/stddef.h>
5#include <linux/bpf.h>
6#include <sys/socket.h>
7
8#include <bpf/bpf_helpers.h>
9#include <bpf/bpf_endian.h>
10
11#include <bpf_sockopt_helpers.h>
12
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
17
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
22
23#define DST_REWRITE_PORT6 6666
24
25SEC("cgroup/sendmsg6")
26int sendmsg_v6_prog(struct bpf_sock_addr *ctx)
27{
28 if (ctx->type != SOCK_DGRAM)
29 return 0;
30
31 if (!get_set_sk_priority(ctx))
32 return 0;
33
34 /* Rewrite source. */
35 if (ctx->msg_src_ip6[3] == bpf_htonl(1) ||
36 ctx->msg_src_ip6[3] == bpf_htonl(0)) {
37 ctx->msg_src_ip6[0] = bpf_htonl(SRC_REWRITE_IP6_0);
38 ctx->msg_src_ip6[1] = bpf_htonl(SRC_REWRITE_IP6_1);
39 ctx->msg_src_ip6[2] = bpf_htonl(SRC_REWRITE_IP6_2);
40 ctx->msg_src_ip6[3] = bpf_htonl(SRC_REWRITE_IP6_3);
41 } else {
42 /* Unexpected source. Reject sendmsg. */
43 return 0;
44 }
45
46 /* Rewrite destination. */
47 if (ctx->user_ip6[0] == bpf_htonl(0xFACEB00C)) {
48 ctx->user_ip6[0] = bpf_htonl(DST_REWRITE_IP6_0);
49 ctx->user_ip6[1] = bpf_htonl(DST_REWRITE_IP6_1);
50 ctx->user_ip6[2] = bpf_htonl(DST_REWRITE_IP6_2);
51 ctx->user_ip6[3] = bpf_htonl(DST_REWRITE_IP6_3);
52
53 ctx->user_port = bpf_htons(DST_REWRITE_PORT6);
54 } else {
55 /* Unexpected destination. Reject sendmsg. */
56 return 0;
57 }
58
59 return 1;
60}
61
62char _license[] SEC("license") = "GPL";