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 #include "bpf_tcp_helpers.h"
19 #define SRC_REWRITE_IP4 0x7f000004U
20 #define DST_REWRITE_IP4 0x7f000001U
21 #define DST_REWRITE_PORT4 4444
23 #ifndef TCP_CA_NAME_MAX
24 #define TCP_CA_NAME_MAX 16
27 #ifndef TCP_NOTSENT_LOWAT
28 #define TCP_NOTSENT_LOWAT 25
35 __attribute__ ((noinline)) __weak
36 int do_bind(struct bpf_sock_addr *ctx)
38 struct sockaddr_in sa = {};
40 sa.sin_family = AF_INET;
41 sa.sin_port = bpf_htons(0);
42 sa.sin_addr.s_addr = bpf_htonl(SRC_REWRITE_IP4);
44 if (bpf_bind(ctx, (struct sockaddr *)&sa, sizeof(sa)) != 0)
50 static __inline int verify_cc(struct bpf_sock_addr *ctx,
51 char expected[TCP_CA_NAME_MAX])
53 char buf[TCP_CA_NAME_MAX];
56 if (bpf_getsockopt(ctx, SOL_TCP, TCP_CONGESTION, &buf, sizeof(buf)))
59 for (i = 0; i < TCP_CA_NAME_MAX; i++) {
60 if (buf[i] != expected[i])
69 static __inline int set_cc(struct bpf_sock_addr *ctx)
71 char reno[TCP_CA_NAME_MAX] = "reno";
72 char cubic[TCP_CA_NAME_MAX] = "cubic";
74 if (bpf_setsockopt(ctx, SOL_TCP, TCP_CONGESTION, &reno, sizeof(reno)))
76 if (verify_cc(ctx, reno))
79 if (bpf_setsockopt(ctx, SOL_TCP, TCP_CONGESTION, &cubic, sizeof(cubic)))
81 if (verify_cc(ctx, cubic))
87 static __inline int bind_to_device(struct bpf_sock_addr *ctx)
89 char veth1[IFNAMSIZ] = "test_sock_addr1";
90 char veth2[IFNAMSIZ] = "test_sock_addr2";
91 char missing[IFNAMSIZ] = "nonexistent_dev";
92 char del_bind[IFNAMSIZ] = "";
94 if (bpf_setsockopt(ctx, SOL_SOCKET, SO_BINDTODEVICE,
95 &veth1, sizeof(veth1)))
97 if (bpf_setsockopt(ctx, SOL_SOCKET, SO_BINDTODEVICE,
98 &veth2, sizeof(veth2)))
100 if (bpf_setsockopt(ctx, SOL_SOCKET, SO_BINDTODEVICE,
101 &missing, sizeof(missing)) != -ENODEV)
103 if (bpf_setsockopt(ctx, SOL_SOCKET, SO_BINDTODEVICE,
104 &del_bind, sizeof(del_bind)))
110 static __inline int set_keepalive(struct bpf_sock_addr *ctx)
112 int zero = 0, one = 1;
114 if (bpf_setsockopt(ctx, SOL_SOCKET, SO_KEEPALIVE, &one, sizeof(one)))
116 if (ctx->type == SOCK_STREAM) {
117 if (bpf_setsockopt(ctx, SOL_TCP, TCP_KEEPIDLE, &one, sizeof(one)))
119 if (bpf_setsockopt(ctx, SOL_TCP, TCP_KEEPINTVL, &one, sizeof(one)))
121 if (bpf_setsockopt(ctx, SOL_TCP, TCP_KEEPCNT, &one, sizeof(one)))
123 if (bpf_setsockopt(ctx, SOL_TCP, TCP_SYNCNT, &one, sizeof(one)))
125 if (bpf_setsockopt(ctx, SOL_TCP, TCP_USER_TIMEOUT, &one, sizeof(one)))
128 if (bpf_setsockopt(ctx, SOL_SOCKET, SO_KEEPALIVE, &zero, sizeof(zero)))
134 static __inline int set_notsent_lowat(struct bpf_sock_addr *ctx)
138 if (ctx->type == SOCK_STREAM) {
139 if (bpf_setsockopt(ctx, SOL_TCP, TCP_NOTSENT_LOWAT, &lowat, sizeof(lowat)))
146 SEC("cgroup/connect4")
147 int connect_v4_prog(struct bpf_sock_addr *ctx)
149 struct bpf_sock_tuple tuple = {};
152 /* Verify that new destination is available. */
153 memset(&tuple.ipv4.saddr, 0, sizeof(tuple.ipv4.saddr));
154 memset(&tuple.ipv4.sport, 0, sizeof(tuple.ipv4.sport));
156 tuple.ipv4.daddr = bpf_htonl(DST_REWRITE_IP4);
157 tuple.ipv4.dport = bpf_htons(DST_REWRITE_PORT4);
159 /* Bind to device and unbind it. */
160 if (bind_to_device(ctx))
163 if (set_keepalive(ctx))
166 if (set_notsent_lowat(ctx))
169 if (ctx->type != SOCK_STREAM && ctx->type != SOCK_DGRAM)
171 else if (ctx->type == SOCK_STREAM)
172 sk = bpf_sk_lookup_tcp(ctx, &tuple, sizeof(tuple.ipv4),
173 BPF_F_CURRENT_NETNS, 0);
175 sk = bpf_sk_lookup_udp(ctx, &tuple, sizeof(tuple.ipv4),
176 BPF_F_CURRENT_NETNS, 0);
181 if (sk->src_ip4 != tuple.ipv4.daddr ||
182 sk->src_port != DST_REWRITE_PORT4) {
189 /* Rewrite congestion control. */
190 if (ctx->type == SOCK_STREAM && set_cc(ctx))
193 /* Rewrite destination. */
194 ctx->user_ip4 = bpf_htonl(DST_REWRITE_IP4);
195 ctx->user_port = bpf_htons(DST_REWRITE_PORT4);
197 return do_bind(ctx) ? 1 : 0;
200 char _license[] SEC("license") = "GPL";