0001
0002
0003 #include <string.h>
0004
0005 #include <linux/stddef.h>
0006 #include <linux/bpf.h>
0007 #include <linux/in.h>
0008 #include <linux/in6.h>
0009 #include <sys/socket.h>
0010 #include <netinet/tcp.h>
0011 #include <linux/if.h>
0012 #include <errno.h>
0013
0014 #include <bpf/bpf_helpers.h>
0015 #include <bpf/bpf_endian.h>
0016
0017 #define SERV6_IP_0 0xfaceb00c
0018 #define SERV6_IP_1 0x12345678
0019 #define SERV6_IP_2 0x00000000
0020 #define SERV6_IP_3 0x0000abcd
0021 #define SERV6_PORT 6060
0022 #define SERV6_REWRITE_IP_0 0x00000000
0023 #define SERV6_REWRITE_IP_1 0x00000000
0024 #define SERV6_REWRITE_IP_2 0x00000000
0025 #define SERV6_REWRITE_IP_3 0x00000001
0026 #define SERV6_REWRITE_PORT 6666
0027
0028 #ifndef IFNAMSIZ
0029 #define IFNAMSIZ 16
0030 #endif
0031
0032 static __inline int bind_to_device(struct bpf_sock_addr *ctx)
0033 {
0034 char veth1[IFNAMSIZ] = "test_sock_addr1";
0035 char veth2[IFNAMSIZ] = "test_sock_addr2";
0036 char missing[IFNAMSIZ] = "nonexistent_dev";
0037 char del_bind[IFNAMSIZ] = "";
0038 int veth1_idx, veth2_idx;
0039
0040 if (bpf_setsockopt(ctx, SOL_SOCKET, SO_BINDTODEVICE,
0041 &veth1, sizeof(veth1)))
0042 return 1;
0043 if (bpf_getsockopt(ctx, SOL_SOCKET, SO_BINDTOIFINDEX,
0044 &veth1_idx, sizeof(veth1_idx)) || !veth1_idx)
0045 return 1;
0046 if (bpf_setsockopt(ctx, SOL_SOCKET, SO_BINDTODEVICE,
0047 &veth2, sizeof(veth2)))
0048 return 1;
0049 if (bpf_getsockopt(ctx, SOL_SOCKET, SO_BINDTOIFINDEX,
0050 &veth2_idx, sizeof(veth2_idx)) || !veth2_idx ||
0051 veth1_idx == veth2_idx)
0052 return 1;
0053 if (bpf_setsockopt(ctx, SOL_SOCKET, SO_BINDTODEVICE,
0054 &missing, sizeof(missing)) != -ENODEV)
0055 return 1;
0056 if (bpf_setsockopt(ctx, SOL_SOCKET, SO_BINDTOIFINDEX,
0057 &veth1_idx, sizeof(veth1_idx)))
0058 return 1;
0059 if (bpf_setsockopt(ctx, SOL_SOCKET, SO_BINDTODEVICE,
0060 &del_bind, sizeof(del_bind)))
0061 return 1;
0062
0063 return 0;
0064 }
0065
0066 static __inline int bind_reuseport(struct bpf_sock_addr *ctx)
0067 {
0068 int val = 1;
0069
0070 if (bpf_setsockopt(ctx, SOL_SOCKET, SO_REUSEPORT,
0071 &val, sizeof(val)))
0072 return 1;
0073 if (bpf_getsockopt(ctx, SOL_SOCKET, SO_REUSEPORT,
0074 &val, sizeof(val)) || !val)
0075 return 1;
0076 val = 0;
0077 if (bpf_setsockopt(ctx, SOL_SOCKET, SO_REUSEPORT,
0078 &val, sizeof(val)))
0079 return 1;
0080 if (bpf_getsockopt(ctx, SOL_SOCKET, SO_REUSEPORT,
0081 &val, sizeof(val)) || val)
0082 return 1;
0083
0084 return 0;
0085 }
0086
0087 static __inline int misc_opts(struct bpf_sock_addr *ctx, int opt)
0088 {
0089 int old, tmp, new = 0xeb9f;
0090
0091
0092 if (bpf_getsockopt(ctx, SOL_SOCKET, opt, &old, sizeof(old)) ||
0093 old == new)
0094 return 1;
0095 if (bpf_setsockopt(ctx, SOL_SOCKET, opt, &new, sizeof(new)))
0096 return 1;
0097 if (bpf_getsockopt(ctx, SOL_SOCKET, opt, &tmp, sizeof(tmp)) ||
0098 tmp != new)
0099 return 1;
0100 if (bpf_setsockopt(ctx, SOL_SOCKET, opt, &old, sizeof(old)))
0101 return 1;
0102
0103 return 0;
0104 }
0105
0106 SEC("cgroup/bind6")
0107 int bind_v6_prog(struct bpf_sock_addr *ctx)
0108 {
0109 struct bpf_sock *sk;
0110 __u32 user_ip6;
0111 __u16 user_port;
0112 int i;
0113
0114 sk = ctx->sk;
0115 if (!sk)
0116 return 0;
0117
0118 if (sk->family != AF_INET6)
0119 return 0;
0120
0121 if (ctx->type != SOCK_STREAM && ctx->type != SOCK_DGRAM)
0122 return 0;
0123
0124 if (ctx->user_ip6[0] != bpf_htonl(SERV6_IP_0) ||
0125 ctx->user_ip6[1] != bpf_htonl(SERV6_IP_1) ||
0126 ctx->user_ip6[2] != bpf_htonl(SERV6_IP_2) ||
0127 ctx->user_ip6[3] != bpf_htonl(SERV6_IP_3) ||
0128 ctx->user_port != bpf_htons(SERV6_PORT))
0129 return 0;
0130
0131
0132 for (i = 0; i < 4; i++) {
0133 user_ip6 = 0;
0134 user_ip6 |= ((volatile __u8 *)&ctx->user_ip6[i])[0] << 0;
0135 user_ip6 |= ((volatile __u8 *)&ctx->user_ip6[i])[1] << 8;
0136 user_ip6 |= ((volatile __u8 *)&ctx->user_ip6[i])[2] << 16;
0137 user_ip6 |= ((volatile __u8 *)&ctx->user_ip6[i])[3] << 24;
0138 if (ctx->user_ip6[i] != user_ip6)
0139 return 0;
0140 }
0141
0142 user_port = 0;
0143 user_port |= ((volatile __u8 *)&ctx->user_port)[0] << 0;
0144 user_port |= ((volatile __u8 *)&ctx->user_port)[1] << 8;
0145 if (ctx->user_port != user_port)
0146 return 0;
0147
0148
0149 for (i = 0; i < 4; i++) {
0150 user_ip6 = 0;
0151 user_ip6 |= ((volatile __u16 *)&ctx->user_ip6[i])[0] << 0;
0152 user_ip6 |= ((volatile __u16 *)&ctx->user_ip6[i])[1] << 16;
0153 if (ctx->user_ip6[i] != user_ip6)
0154 return 0;
0155 }
0156
0157
0158 if (bind_to_device(ctx))
0159 return 0;
0160
0161
0162 if (misc_opts(ctx, SO_MARK) || misc_opts(ctx, SO_PRIORITY))
0163 return 0;
0164
0165
0166 if (bind_reuseport(ctx))
0167 return 0;
0168
0169 ctx->user_ip6[0] = bpf_htonl(SERV6_REWRITE_IP_0);
0170 ctx->user_ip6[1] = bpf_htonl(SERV6_REWRITE_IP_1);
0171 ctx->user_ip6[2] = bpf_htonl(SERV6_REWRITE_IP_2);
0172 ctx->user_ip6[3] = bpf_htonl(SERV6_REWRITE_IP_3);
0173 ctx->user_port = bpf_htons(SERV6_REWRITE_PORT);
0174
0175 return 1;
0176 }
0177
0178 char _license[] SEC("license") = "GPL";