xref: /linux/net/ipv4/bpf_tcp_ca.c (revision a1a9b71ebd6582c94090ee5b70fb3eabf7088f22)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2019 Facebook  */
3 
4 #include <linux/init.h>
5 #include <linux/types.h>
6 #include <linux/bpf_verifier.h>
7 #include <linux/bpf.h>
8 #include <linux/btf.h>
9 #include <linux/btf_ids.h>
10 #include <linux/filter.h>
11 #include <net/tcp.h>
12 #include <net/bpf_sk_storage.h>
13 
14 /* "extern" is to avoid sparse warning.  It is only used in bpf_struct_ops.c. */
15 extern struct bpf_struct_ops bpf_tcp_congestion_ops;
16 
17 static u32 optional_ops[] = {
18 	offsetof(struct tcp_congestion_ops, init),
19 	offsetof(struct tcp_congestion_ops, release),
20 	offsetof(struct tcp_congestion_ops, set_state),
21 	offsetof(struct tcp_congestion_ops, cwnd_event),
22 	offsetof(struct tcp_congestion_ops, in_ack_event),
23 	offsetof(struct tcp_congestion_ops, pkts_acked),
24 	offsetof(struct tcp_congestion_ops, min_tso_segs),
25 	offsetof(struct tcp_congestion_ops, sndbuf_expand),
26 	offsetof(struct tcp_congestion_ops, cong_control),
27 };
28 
29 static u32 unsupported_ops[] = {
30 	offsetof(struct tcp_congestion_ops, get_info),
31 };
32 
33 static const struct btf_type *tcp_sock_type;
34 static u32 tcp_sock_id, sock_id;
35 
36 static int bpf_tcp_ca_init(struct btf *btf)
37 {
38 	s32 type_id;
39 
40 	type_id = btf_find_by_name_kind(btf, "sock", BTF_KIND_STRUCT);
41 	if (type_id < 0)
42 		return -EINVAL;
43 	sock_id = type_id;
44 
45 	type_id = btf_find_by_name_kind(btf, "tcp_sock", BTF_KIND_STRUCT);
46 	if (type_id < 0)
47 		return -EINVAL;
48 	tcp_sock_id = type_id;
49 	tcp_sock_type = btf_type_by_id(btf, tcp_sock_id);
50 
51 	return 0;
52 }
53 
54 static bool is_optional(u32 member_offset)
55 {
56 	unsigned int i;
57 
58 	for (i = 0; i < ARRAY_SIZE(optional_ops); i++) {
59 		if (member_offset == optional_ops[i])
60 			return true;
61 	}
62 
63 	return false;
64 }
65 
66 static bool is_unsupported(u32 member_offset)
67 {
68 	unsigned int i;
69 
70 	for (i = 0; i < ARRAY_SIZE(unsupported_ops); i++) {
71 		if (member_offset == unsupported_ops[i])
72 			return true;
73 	}
74 
75 	return false;
76 }
77 
78 extern struct btf *btf_vmlinux;
79 
80 static bool bpf_tcp_ca_is_valid_access(int off, int size,
81 				       enum bpf_access_type type,
82 				       const struct bpf_prog *prog,
83 				       struct bpf_insn_access_aux *info)
84 {
85 	if (!bpf_tracing_btf_ctx_access(off, size, type, prog, info))
86 		return false;
87 
88 	if (info->reg_type == PTR_TO_BTF_ID && info->btf_id == sock_id)
89 		/* promote it to tcp_sock */
90 		info->btf_id = tcp_sock_id;
91 
92 	return true;
93 }
94 
95 static int bpf_tcp_ca_btf_struct_access(struct bpf_verifier_log *log,
96 					const struct btf *btf,
97 					const struct btf_type *t, int off,
98 					int size, enum bpf_access_type atype,
99 					u32 *next_btf_id,
100 					enum bpf_type_flag *flag)
101 {
102 	size_t end;
103 
104 	if (atype == BPF_READ)
105 		return btf_struct_access(log, btf, t, off, size, atype, next_btf_id,
106 					 flag);
107 
108 	if (t != tcp_sock_type) {
109 		bpf_log(log, "only read is supported\n");
110 		return -EACCES;
111 	}
112 
113 	switch (off) {
114 	case bpf_ctx_range(struct inet_connection_sock, icsk_ca_priv):
115 		end = offsetofend(struct inet_connection_sock, icsk_ca_priv);
116 		break;
117 	case offsetof(struct inet_connection_sock, icsk_ack.pending):
118 		end = offsetofend(struct inet_connection_sock,
119 				  icsk_ack.pending);
120 		break;
121 	case offsetof(struct tcp_sock, snd_cwnd):
122 		end = offsetofend(struct tcp_sock, snd_cwnd);
123 		break;
124 	case offsetof(struct tcp_sock, snd_cwnd_cnt):
125 		end = offsetofend(struct tcp_sock, snd_cwnd_cnt);
126 		break;
127 	case offsetof(struct tcp_sock, snd_ssthresh):
128 		end = offsetofend(struct tcp_sock, snd_ssthresh);
129 		break;
130 	case offsetof(struct tcp_sock, ecn_flags):
131 		end = offsetofend(struct tcp_sock, ecn_flags);
132 		break;
133 	default:
134 		bpf_log(log, "no write support to tcp_sock at off %d\n", off);
135 		return -EACCES;
136 	}
137 
138 	if (off + size > end) {
139 		bpf_log(log,
140 			"write access at off %d with size %d beyond the member of tcp_sock ended at %zu\n",
141 			off, size, end);
142 		return -EACCES;
143 	}
144 
145 	return NOT_INIT;
146 }
147 
148 BPF_CALL_2(bpf_tcp_send_ack, struct tcp_sock *, tp, u32, rcv_nxt)
149 {
150 	/* bpf_tcp_ca prog cannot have NULL tp */
151 	__tcp_send_ack((struct sock *)tp, rcv_nxt);
152 	return 0;
153 }
154 
155 static const struct bpf_func_proto bpf_tcp_send_ack_proto = {
156 	.func		= bpf_tcp_send_ack,
157 	.gpl_only	= false,
158 	/* In case we want to report error later */
159 	.ret_type	= RET_INTEGER,
160 	.arg1_type	= ARG_PTR_TO_BTF_ID,
161 	.arg1_btf_id	= &tcp_sock_id,
162 	.arg2_type	= ARG_ANYTHING,
163 };
164 
165 static u32 prog_ops_moff(const struct bpf_prog *prog)
166 {
167 	const struct btf_member *m;
168 	const struct btf_type *t;
169 	u32 midx;
170 
171 	midx = prog->expected_attach_type;
172 	t = bpf_tcp_congestion_ops.type;
173 	m = &btf_type_member(t)[midx];
174 
175 	return __btf_member_bit_offset(t, m) / 8;
176 }
177 
178 static const struct bpf_func_proto *
179 bpf_tcp_ca_get_func_proto(enum bpf_func_id func_id,
180 			  const struct bpf_prog *prog)
181 {
182 	switch (func_id) {
183 	case BPF_FUNC_tcp_send_ack:
184 		return &bpf_tcp_send_ack_proto;
185 	case BPF_FUNC_sk_storage_get:
186 		return &bpf_sk_storage_get_proto;
187 	case BPF_FUNC_sk_storage_delete:
188 		return &bpf_sk_storage_delete_proto;
189 	case BPF_FUNC_setsockopt:
190 		/* Does not allow release() to call setsockopt.
191 		 * release() is called when the current bpf-tcp-cc
192 		 * is retiring.  It is not allowed to call
193 		 * setsockopt() to make further changes which
194 		 * may potentially allocate new resources.
195 		 */
196 		if (prog_ops_moff(prog) !=
197 		    offsetof(struct tcp_congestion_ops, release))
198 			return &bpf_sk_setsockopt_proto;
199 		return NULL;
200 	case BPF_FUNC_getsockopt:
201 		/* Since get/setsockopt is usually expected to
202 		 * be available together, disable getsockopt for
203 		 * release also to avoid usage surprise.
204 		 * The bpf-tcp-cc already has a more powerful way
205 		 * to read tcp_sock from the PTR_TO_BTF_ID.
206 		 */
207 		if (prog_ops_moff(prog) !=
208 		    offsetof(struct tcp_congestion_ops, release))
209 			return &bpf_sk_getsockopt_proto;
210 		return NULL;
211 	case BPF_FUNC_ktime_get_coarse_ns:
212 		return &bpf_ktime_get_coarse_ns_proto;
213 	default:
214 		return bpf_base_func_proto(func_id);
215 	}
216 }
217 
218 BTF_SET_START(bpf_tcp_ca_check_kfunc_ids)
219 BTF_ID(func, tcp_reno_ssthresh)
220 BTF_ID(func, tcp_reno_cong_avoid)
221 BTF_ID(func, tcp_reno_undo_cwnd)
222 BTF_ID(func, tcp_slow_start)
223 BTF_ID(func, tcp_cong_avoid_ai)
224 BTF_SET_END(bpf_tcp_ca_check_kfunc_ids)
225 
226 static const struct btf_kfunc_id_set bpf_tcp_ca_kfunc_set = {
227 	.owner     = THIS_MODULE,
228 	.check_set = &bpf_tcp_ca_check_kfunc_ids,
229 };
230 
231 static const struct bpf_verifier_ops bpf_tcp_ca_verifier_ops = {
232 	.get_func_proto		= bpf_tcp_ca_get_func_proto,
233 	.is_valid_access	= bpf_tcp_ca_is_valid_access,
234 	.btf_struct_access	= bpf_tcp_ca_btf_struct_access,
235 };
236 
237 static int bpf_tcp_ca_init_member(const struct btf_type *t,
238 				  const struct btf_member *member,
239 				  void *kdata, const void *udata)
240 {
241 	const struct tcp_congestion_ops *utcp_ca;
242 	struct tcp_congestion_ops *tcp_ca;
243 	int prog_fd;
244 	u32 moff;
245 
246 	utcp_ca = (const struct tcp_congestion_ops *)udata;
247 	tcp_ca = (struct tcp_congestion_ops *)kdata;
248 
249 	moff = __btf_member_bit_offset(t, member) / 8;
250 	switch (moff) {
251 	case offsetof(struct tcp_congestion_ops, flags):
252 		if (utcp_ca->flags & ~TCP_CONG_MASK)
253 			return -EINVAL;
254 		tcp_ca->flags = utcp_ca->flags;
255 		return 1;
256 	case offsetof(struct tcp_congestion_ops, name):
257 		if (bpf_obj_name_cpy(tcp_ca->name, utcp_ca->name,
258 				     sizeof(tcp_ca->name)) <= 0)
259 			return -EINVAL;
260 		if (tcp_ca_find(utcp_ca->name))
261 			return -EEXIST;
262 		return 1;
263 	}
264 
265 	if (!btf_type_resolve_func_ptr(btf_vmlinux, member->type, NULL))
266 		return 0;
267 
268 	/* Ensure bpf_prog is provided for compulsory func ptr */
269 	prog_fd = (int)(*(unsigned long *)(udata + moff));
270 	if (!prog_fd && !is_optional(moff) && !is_unsupported(moff))
271 		return -EINVAL;
272 
273 	return 0;
274 }
275 
276 static int bpf_tcp_ca_check_member(const struct btf_type *t,
277 				   const struct btf_member *member)
278 {
279 	if (is_unsupported(__btf_member_bit_offset(t, member) / 8))
280 		return -ENOTSUPP;
281 	return 0;
282 }
283 
284 static int bpf_tcp_ca_reg(void *kdata)
285 {
286 	return tcp_register_congestion_control(kdata);
287 }
288 
289 static void bpf_tcp_ca_unreg(void *kdata)
290 {
291 	tcp_unregister_congestion_control(kdata);
292 }
293 
294 struct bpf_struct_ops bpf_tcp_congestion_ops = {
295 	.verifier_ops = &bpf_tcp_ca_verifier_ops,
296 	.reg = bpf_tcp_ca_reg,
297 	.unreg = bpf_tcp_ca_unreg,
298 	.check_member = bpf_tcp_ca_check_member,
299 	.init_member = bpf_tcp_ca_init_member,
300 	.init = bpf_tcp_ca_init,
301 	.name = "tcp_congestion_ops",
302 };
303 
304 static int __init bpf_tcp_ca_kfunc_init(void)
305 {
306 	return register_btf_kfunc_id_set(BPF_PROG_TYPE_STRUCT_OPS, &bpf_tcp_ca_kfunc_set);
307 }
308 late_initcall(bpf_tcp_ca_kfunc_init);
309