1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
|
// SPDX-License-Identifier: GPL-2.0
/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */
#include <linux/bpf.h>
#include <linux/btf_ids.h>
#include <linux/bpf_verifier.h>
#include <linux/filter.h>
#include <net/bpf_sk_storage.h>
#include <net/tcp.h>
static int timeout_init_stub(struct sock *sk, struct request_sock *req__nullable)
{
struct bpf_tramp_run_ctx *ctx =
container_of(current->bpf_ctx, struct bpf_tramp_run_ctx, run_ctx);
return ctx->retval;
}
static int rwnd_init_stub(struct sock *sk, struct request_sock *req__nullable)
{
struct bpf_tramp_run_ctx *ctx =
container_of(current->bpf_ctx, struct bpf_tramp_run_ctx, run_ctx);
return ctx->retval;
}
static void active_established_stub(struct sock *sk, struct sk_buff *skb__nullable)
{
}
static void passive_established_stub(struct sock *sk, struct sk_buff *skb)
{
}
static void rto_stub(struct sock *sk)
{
}
static void rtt_stub(struct sock *sk, long mrtt, u32 srtt)
{
}
static void set_state_stub(struct sock *sk, int state)
{
}
static void retrans_stub(struct sock *sk, struct sk_buff *skb, int err)
{
}
static void connect_stub(struct sock *sk)
{
}
static void listen_stub(struct sock *sk)
{
}
static void parse_hdr_stub(struct sock *sk, struct sk_buff *skb)
{
}
static void hdr_opt_len_stub(struct sock *sk, struct sk_buff *skb__nullable,
struct request_sock *req__nullable,
struct sk_buff *syn_skb__nullable,
enum tcp_synack_type synack_type,
unsigned int *remaining)
{
}
static void write_hdr_opt_stub(struct sock *sk, struct sk_buff *skb,
struct request_sock *req__nullable,
struct sk_buff *syn_skb__nullable,
enum tcp_synack_type synack_type,
u32 opt_off)
{
}
static struct bpf_tcp_ops __bpf_tcp_ops = {
.timeout_init = timeout_init_stub,
.rwnd_init = rwnd_init_stub,
.active_established = active_established_stub,
.passive_established = passive_established_stub,
.rto = rto_stub,
.rtt = rtt_stub,
.set_state = set_state_stub,
.retrans = retrans_stub,
.connect = connect_stub,
.listen = listen_stub,
.parse_hdr = parse_hdr_stub,
.hdr_opt_len = hdr_opt_len_stub,
.write_hdr_opt = write_hdr_opt_stub,
};
BPF_CALL_4(bpf_tcp_ops_store_hdr_opt, void *, ctx, const void *, from,
u32, len, u64, flags)
{
u64 *args = ctx;
struct sk_buff *skb = (void *)(unsigned long)args[1];
struct bpf_sock_ops_kern sock_ops = {};
u32 opt_off = args[5];
u8 *op, *opend;
/*
* bpf_tcp_ops does not keep track of the end of the written TCP header
* options, so search for it every time the helper is called. The free
* space is NOP-filled, so a TCPOPT_NOP ends the search rather than being
* skipped as in a normal option walk in sockops.
*/
op = skb->data + opt_off;
opend = skb->data + tcp_hdrlen(skb);
while (op < opend && *op != TCPOPT_NOP) {
if (*op == TCPOPT_EOL || op + 1 >= opend || op[1] < 2)
break;
op += op[1];
}
sock_ops.skb = skb;
sock_ops.skb_data_end = op;
sock_ops.remaining_opt_len = opend - op;
return __bpf_sock_ops_store_hdr_opt(&sock_ops, from, len, flags);
}
static const struct bpf_func_proto bpf_tcp_ops_store_hdr_opt_proto = {
.func = bpf_tcp_ops_store_hdr_opt,
.gpl_only = false,
.ret_type = RET_INTEGER,
.arg1_type = ARG_PTR_TO_CTX,
.arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
.arg3_type = ARG_MEM_SIZE,
.arg4_type = ARG_ANYTHING,
};
BPF_CALL_4(bpf_tcp_ops_load_hdr_opt, void *, ctx, void *, search_res,
u32, len, u64, flags)
{
u64 *args = ctx;
struct sk_buff *skb = (void *)(unsigned long)args[1];
struct bpf_sock_ops_kern sock_ops = {};
/*
* No flags supported. In particular BPF_LOAD_HDR_OPT_TCP_SYN, which
* loads from the saved SYN, is not available because bpf_tcp_ops has no
* carrier to track the SYN source across the hooks.
*/
if (flags)
return -EINVAL;
sock_ops.skb = skb;
sock_ops.skb_data_end = skb->data + tcp_hdrlen(skb);
return __bpf_sock_ops_load_hdr_opt(&sock_ops, search_res, len, flags);
}
static const struct bpf_func_proto bpf_tcp_ops_load_hdr_opt_proto = {
.func = bpf_tcp_ops_load_hdr_opt,
.gpl_only = false,
.ret_type = RET_INTEGER,
.arg1_type = ARG_PTR_TO_CTX,
.arg2_type = ARG_PTR_TO_MEM | MEM_WRITE,
.arg3_type = ARG_MEM_SIZE,
.arg4_type = ARG_ANYTHING,
};
BPF_CALL_3(bpf_tcp_ops_reserve_hdr_opt, void *, ctx, u32, len, u64, flags)
{
u64 *args = ctx;
unsigned int *remaining = (void *)(unsigned long)args[5];
if (flags || len < 2)
return -EINVAL;
if (len > *remaining)
return -ENOSPC;
*remaining -= len;
return 0;
}
static const struct bpf_func_proto bpf_tcp_ops_reserve_hdr_opt_proto = {
.func = bpf_tcp_ops_reserve_hdr_opt,
.gpl_only = false,
.ret_type = RET_INTEGER,
.arg1_type = ARG_PTR_TO_CTX,
.arg2_type = ARG_ANYTHING,
.arg3_type = ARG_ANYTHING,
};
BPF_CALL_0(bpf_tcp_ops_get_retval)
{
struct bpf_tramp_run_ctx *ctx =
container_of(current->bpf_ctx, struct bpf_tramp_run_ctx, run_ctx);
/* bpf_get_retval() is only exposed to timeout_init/rwnd_init, which
* always run via bpf_tcp_ops_call_int(). Its run_ctx carries the int
* return value chained across the bpf_tcp_ops attached to the cgroup
* and is this program's saved_run_ctx.
*/
if (WARN_ON_ONCE(!ctx->saved_run_ctx))
return 0;
return container_of(ctx->saved_run_ctx, struct bpf_tramp_run_ctx,
run_ctx)->retval;
}
const struct bpf_func_proto bpf_tcp_ops_get_retval_proto = {
.func = bpf_tcp_ops_get_retval,
.gpl_only = false,
.ret_type = RET_INTEGER,
};
static const struct bpf_func_proto *
get_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
{
u32 moff = prog->aux->attach_st_ops_member_off;
switch (func_id) {
case BPF_FUNC_sk_storage_get:
return &bpf_sk_storage_get_proto;
case BPF_FUNC_sk_storage_delete:
return &bpf_sk_storage_delete_proto;
case BPF_FUNC_setsockopt:
/* The sk may be an unlocked listener (synack path) or NULL
* fullsock; disable for members that can run unlocked.
*/
if (moff == offsetof(struct bpf_tcp_ops, rwnd_init) ||
moff == offsetof(struct bpf_tcp_ops, timeout_init) ||
moff == offsetof(struct bpf_tcp_ops, hdr_opt_len) ||
moff == offsetof(struct bpf_tcp_ops, write_hdr_opt))
return NULL;
return &bpf_sk_setsockopt_proto;
case BPF_FUNC_getsockopt:
if (moff == offsetof(struct bpf_tcp_ops, rwnd_init) ||
moff == offsetof(struct bpf_tcp_ops, timeout_init) ||
moff == offsetof(struct bpf_tcp_ops, hdr_opt_len) ||
moff == offsetof(struct bpf_tcp_ops, write_hdr_opt))
return NULL;
return &bpf_sk_getsockopt_proto;
case BPF_FUNC_get_retval:
if (moff == offsetof(struct bpf_tcp_ops, timeout_init) ||
moff == offsetof(struct bpf_tcp_ops, rwnd_init))
return &bpf_tcp_ops_get_retval_proto;
return NULL;
case BPF_FUNC_reserve_hdr_opt:
if (moff == offsetof(struct bpf_tcp_ops, hdr_opt_len))
return &bpf_tcp_ops_reserve_hdr_opt_proto;
return NULL;
case BPF_FUNC_load_hdr_opt:
if (moff == offsetof(struct bpf_tcp_ops, parse_hdr) ||
moff == offsetof(struct bpf_tcp_ops, write_hdr_opt))
return &bpf_tcp_ops_load_hdr_opt_proto;
return NULL;
case BPF_FUNC_store_hdr_opt:
if (moff == offsetof(struct bpf_tcp_ops, write_hdr_opt))
return &bpf_tcp_ops_store_hdr_opt_proto;
return NULL;
default:
return bpf_base_func_proto(func_id, prog);
}
}
static bool is_valid_access(int off, int size, enum bpf_access_type type,
const struct bpf_prog *prog, struct bpf_insn_access_aux *info)
{
if (!bpf_tracing_btf_ctx_access(off, size, type, prog, info))
return false;
if (base_type(info->reg_type) == PTR_TO_BTF_ID &&
!bpf_type_has_unsafe_modifiers(info->reg_type) &&
info->btf_id == btf_sock_ids[BTF_SOCK_TYPE_SOCK])
/* promote it to tcp_sock */
info->btf_id = btf_sock_ids[BTF_SOCK_TYPE_TCP];
return true;
}
static int bpf_tcp_ops_init_member(const struct btf_type *t,
const struct btf_member *member,
void *kdata, const void *udata)
{
return 0;
}
static int bpf_tcp_ops_check_member(const struct btf_type *t,
const struct btf_member *member,
const struct bpf_prog *prog)
{
if (prog->sleepable)
return -EINVAL;
return 0;
}
static int bpf_tcp_ops_init(struct btf *btf)
{
return 0;
}
static int bpf_tcp_ops_validate(void *kdata)
{
return 0;
}
static const struct bpf_verifier_ops bpf_tcp_ops_verifier = {
.get_func_proto = get_func_proto,
.is_valid_access = is_valid_access,
};
static struct bpf_struct_ops bpf_tcp_ops = {
.verifier_ops = &bpf_tcp_ops_verifier,
.init_member = bpf_tcp_ops_init_member,
.check_member = bpf_tcp_ops_check_member,
.init = bpf_tcp_ops_init,
.validate = bpf_tcp_ops_validate,
.name = "bpf_tcp_ops",
.cgroup_atype = CGROUP_TCP_SOCK_OPS,
.cfi_stubs = &__bpf_tcp_ops,
.owner = THIS_MODULE,
};
static int __init __bpf_tcp_ops_init(void)
{
return register_bpf_struct_ops(&bpf_tcp_ops, bpf_tcp_ops);
}
late_initcall(__bpf_tcp_ops_init);
|