Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 // Copyright (c) 2018 Facebook
0003 
0004 #include <linux/stddef.h>
0005 #include <linux/bpf.h>
0006 #include <sys/socket.h>
0007 
0008 #include <bpf/bpf_helpers.h>
0009 #include <bpf/bpf_endian.h>
0010 
0011 #include <bpf_sockopt_helpers.h>
0012 
0013 #define SRC_REWRITE_IP6_0   0
0014 #define SRC_REWRITE_IP6_1   0
0015 #define SRC_REWRITE_IP6_2   0
0016 #define SRC_REWRITE_IP6_3   6
0017 
0018 #define DST_REWRITE_IP6_0   0
0019 #define DST_REWRITE_IP6_1   0
0020 #define DST_REWRITE_IP6_2   0
0021 #define DST_REWRITE_IP6_3   1
0022 
0023 #define DST_REWRITE_PORT6   6666
0024 
0025 SEC("cgroup/sendmsg6")
0026 int sendmsg_v6_prog(struct bpf_sock_addr *ctx)
0027 {
0028     if (ctx->type != SOCK_DGRAM)
0029         return 0;
0030 
0031     if (!get_set_sk_priority(ctx))
0032         return 0;
0033 
0034     /* Rewrite source. */
0035     if (ctx->msg_src_ip6[3] == bpf_htonl(1) ||
0036         ctx->msg_src_ip6[3] == bpf_htonl(0)) {
0037         ctx->msg_src_ip6[0] = bpf_htonl(SRC_REWRITE_IP6_0);
0038         ctx->msg_src_ip6[1] = bpf_htonl(SRC_REWRITE_IP6_1);
0039         ctx->msg_src_ip6[2] = bpf_htonl(SRC_REWRITE_IP6_2);
0040         ctx->msg_src_ip6[3] = bpf_htonl(SRC_REWRITE_IP6_3);
0041     } else {
0042         /* Unexpected source. Reject sendmsg. */
0043         return 0;
0044     }
0045 
0046     /* Rewrite destination. */
0047     if (ctx->user_ip6[0] == bpf_htonl(0xFACEB00C)) {
0048         ctx->user_ip6[0] = bpf_htonl(DST_REWRITE_IP6_0);
0049         ctx->user_ip6[1] = bpf_htonl(DST_REWRITE_IP6_1);
0050         ctx->user_ip6[2] = bpf_htonl(DST_REWRITE_IP6_2);
0051         ctx->user_ip6[3] = bpf_htonl(DST_REWRITE_IP6_3);
0052 
0053         ctx->user_port = bpf_htons(DST_REWRITE_PORT6);
0054     } else {
0055         /* Unexpected destination. Reject sendmsg. */
0056         return 0;
0057     }
0058 
0059     return 1;
0060 }
0061 
0062 char _license[] SEC("license") = "GPL";