1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (c) 2018 Facebook
6 #include <linux/stddef.h>
10 #include <linux/tcp.h>
14 #include <bpf/bpf_helpers.h>
15 #include <bpf/bpf_endian.h>
17 #define SRC_REWRITE_IP4 0x7f000004U
18 #define DST_REWRITE_IP4 0x7f000001U
19 #define DST_REWRITE_PORT4 4444
21 #ifndef TCP_CA_NAME_MAX
22 #define TCP_CA_NAME_MAX 16
25 #ifndef TCP_NOTSENT_LOWAT
26 #define TCP_NOTSENT_LOWAT 25
37 __attribute__ ((noinline)) __weak
38 int do_bind(struct bpf_sock_addr *ctx)
40 struct sockaddr_in sa = {};
42 sa.sin_family = AF_INET;
43 sa.sin_port = bpf_htons(0);
44 sa.sin_addr.s_addr = bpf_htonl(SRC_REWRITE_IP4);
46 if (bpf_bind(ctx, (struct sockaddr *)&sa, sizeof(sa)) != 0)
52 static __inline int verify_cc(struct bpf_sock_addr *ctx,
53 char expected[TCP_CA_NAME_MAX])
55 char buf[TCP_CA_NAME_MAX];
58 if (bpf_getsockopt(ctx, SOL_TCP, TCP_CONGESTION, &buf, sizeof(buf)))
61 for (i = 0; i < TCP_CA_NAME_MAX; i++) {
62 if (buf[i] != expected[i])
71 static __inline int set_cc(struct bpf_sock_addr *ctx)
73 char reno[TCP_CA_NAME_MAX] = "reno";
74 char cubic[TCP_CA_NAME_MAX] = "cubic";
76 if (bpf_setsockopt(ctx, SOL_TCP, TCP_CONGESTION, &reno, sizeof(reno)))
78 if (verify_cc(ctx, reno))
81 if (bpf_setsockopt(ctx, SOL_TCP, TCP_CONGESTION, &cubic, sizeof(cubic)))
83 if (verify_cc(ctx, cubic))
89 static __inline int bind_to_device(struct bpf_sock_addr *ctx)
91 char veth1[IFNAMSIZ] = "test_sock_addr1";
92 char veth2[IFNAMSIZ] = "test_sock_addr2";
93 char missing[IFNAMSIZ] = "nonexistent_dev";
94 char del_bind[IFNAMSIZ] = "";
96 if (bpf_setsockopt(ctx, SOL_SOCKET, SO_BINDTODEVICE,
97 &veth1, sizeof(veth1)))
99 if (bpf_setsockopt(ctx, SOL_SOCKET, SO_BINDTODEVICE,
100 &veth2, sizeof(veth2)))
102 if (bpf_setsockopt(ctx, SOL_SOCKET, SO_BINDTODEVICE,
103 &missing, sizeof(missing)) != -ENODEV)
105 if (bpf_setsockopt(ctx, SOL_SOCKET, SO_BINDTODEVICE,
106 &del_bind, sizeof(del_bind)))
112 static __inline int set_keepalive(struct bpf_sock_addr *ctx)
114 int zero = 0, one = 1;
116 if (bpf_setsockopt(ctx, SOL_SOCKET, SO_KEEPALIVE, &one, sizeof(one)))
118 if (ctx->type == SOCK_STREAM) {
119 if (bpf_setsockopt(ctx, SOL_TCP, TCP_KEEPIDLE, &one, sizeof(one)))
121 if (bpf_setsockopt(ctx, SOL_TCP, TCP_KEEPINTVL, &one, sizeof(one)))
123 if (bpf_setsockopt(ctx, SOL_TCP, TCP_KEEPCNT, &one, sizeof(one)))
125 if (bpf_setsockopt(ctx, SOL_TCP, TCP_SYNCNT, &one, sizeof(one)))
127 if (bpf_setsockopt(ctx, SOL_TCP, TCP_USER_TIMEOUT, &one, sizeof(one)))
130 if (bpf_setsockopt(ctx, SOL_SOCKET, SO_KEEPALIVE, &zero, sizeof(zero)))
136 static __inline int set_notsent_lowat(struct bpf_sock_addr *ctx)
140 if (ctx->type == SOCK_STREAM) {
141 if (bpf_setsockopt(ctx, SOL_TCP, TCP_NOTSENT_LOWAT, &lowat, sizeof(lowat)))
148 SEC("cgroup/connect4")
149 int connect_v4_prog(struct bpf_sock_addr *ctx)
151 struct bpf_sock_tuple tuple = {};
154 /* Verify that new destination is available. */
155 memset(&tuple.ipv4.saddr, 0, sizeof(tuple.ipv4.saddr));
156 memset(&tuple.ipv4.sport, 0, sizeof(tuple.ipv4.sport));
158 tuple.ipv4.daddr = bpf_htonl(DST_REWRITE_IP4);
159 tuple.ipv4.dport = bpf_htons(DST_REWRITE_PORT4);
161 /* Bind to device and unbind it. */
162 if (bind_to_device(ctx))
165 if (set_keepalive(ctx))
168 if (set_notsent_lowat(ctx))
171 if (ctx->type != SOCK_STREAM && ctx->type != SOCK_DGRAM)
173 else if (ctx->type == SOCK_STREAM)
174 sk = bpf_sk_lookup_tcp(ctx, &tuple, sizeof(tuple.ipv4),
175 BPF_F_CURRENT_NETNS, 0);
177 sk = bpf_sk_lookup_udp(ctx, &tuple, sizeof(tuple.ipv4),
178 BPF_F_CURRENT_NETNS, 0);
183 if (sk->src_ip4 != tuple.ipv4.daddr ||
184 sk->src_port != DST_REWRITE_PORT4) {
191 /* Rewrite congestion control. */
192 if (ctx->type == SOCK_STREAM && set_cc(ctx))
195 /* Rewrite destination. */
196 ctx->user_ip4 = bpf_htonl(DST_REWRITE_IP4);
197 ctx->user_port = bpf_htons(DST_REWRITE_PORT4);
199 return do_bind(ctx) ? 1 : 0;
202 SEC("cgroup/connect4")
203 int connect_v4_deny_prog(struct bpf_sock_addr *ctx)
208 char _license[] SEC("license") = "GPL";