xref: /linux/net/ipv4/bpf_tcp_ca.c (revision 5e0bc3082e2e403ac0753e099c2b01446bb35578)
10baf26b0SMartin KaFai Lau // SPDX-License-Identifier: GPL-2.0
20baf26b0SMartin KaFai Lau /* Copyright (c) 2019 Facebook  */
30baf26b0SMartin KaFai Lau 
40baf26b0SMartin KaFai Lau #include <linux/types.h>
50baf26b0SMartin KaFai Lau #include <linux/bpf_verifier.h>
60baf26b0SMartin KaFai Lau #include <linux/bpf.h>
70baf26b0SMartin KaFai Lau #include <linux/btf.h>
8e78aea8bSMartin KaFai Lau #include <linux/btf_ids.h>
90baf26b0SMartin KaFai Lau #include <linux/filter.h>
100baf26b0SMartin KaFai Lau #include <net/tcp.h>
11ab14fd4eSMartin KaFai Lau #include <net/bpf_sk_storage.h>
120baf26b0SMartin KaFai Lau 
13eb18b49eSMartin KaFai Lau /* "extern" is to avoid sparse warning.  It is only used in bpf_struct_ops.c. */
14eb18b49eSMartin KaFai Lau extern struct bpf_struct_ops bpf_tcp_congestion_ops;
15eb18b49eSMartin KaFai Lau 
160baf26b0SMartin KaFai Lau static u32 optional_ops[] = {
170baf26b0SMartin KaFai Lau 	offsetof(struct tcp_congestion_ops, init),
180baf26b0SMartin KaFai Lau 	offsetof(struct tcp_congestion_ops, release),
190baf26b0SMartin KaFai Lau 	offsetof(struct tcp_congestion_ops, set_state),
200baf26b0SMartin KaFai Lau 	offsetof(struct tcp_congestion_ops, cwnd_event),
210baf26b0SMartin KaFai Lau 	offsetof(struct tcp_congestion_ops, in_ack_event),
220baf26b0SMartin KaFai Lau 	offsetof(struct tcp_congestion_ops, pkts_acked),
230baf26b0SMartin KaFai Lau 	offsetof(struct tcp_congestion_ops, min_tso_segs),
240baf26b0SMartin KaFai Lau 	offsetof(struct tcp_congestion_ops, sndbuf_expand),
250baf26b0SMartin KaFai Lau 	offsetof(struct tcp_congestion_ops, cong_control),
260baf26b0SMartin KaFai Lau };
270baf26b0SMartin KaFai Lau 
280baf26b0SMartin KaFai Lau static u32 unsupported_ops[] = {
290baf26b0SMartin KaFai Lau 	offsetof(struct tcp_congestion_ops, get_info),
300baf26b0SMartin KaFai Lau };
310baf26b0SMartin KaFai Lau 
320baf26b0SMartin KaFai Lau static const struct btf_type *tcp_sock_type;
330baf26b0SMartin KaFai Lau static u32 tcp_sock_id, sock_id;
340baf26b0SMartin KaFai Lau 
350baf26b0SMartin KaFai Lau static int bpf_tcp_ca_init(struct btf *btf)
360baf26b0SMartin KaFai Lau {
370baf26b0SMartin KaFai Lau 	s32 type_id;
380baf26b0SMartin KaFai Lau 
390baf26b0SMartin KaFai Lau 	type_id = btf_find_by_name_kind(btf, "sock", BTF_KIND_STRUCT);
400baf26b0SMartin KaFai Lau 	if (type_id < 0)
410baf26b0SMartin KaFai Lau 		return -EINVAL;
420baf26b0SMartin KaFai Lau 	sock_id = type_id;
430baf26b0SMartin KaFai Lau 
440baf26b0SMartin KaFai Lau 	type_id = btf_find_by_name_kind(btf, "tcp_sock", BTF_KIND_STRUCT);
450baf26b0SMartin KaFai Lau 	if (type_id < 0)
460baf26b0SMartin KaFai Lau 		return -EINVAL;
470baf26b0SMartin KaFai Lau 	tcp_sock_id = type_id;
480baf26b0SMartin KaFai Lau 	tcp_sock_type = btf_type_by_id(btf, tcp_sock_id);
490baf26b0SMartin KaFai Lau 
500baf26b0SMartin KaFai Lau 	return 0;
510baf26b0SMartin KaFai Lau }
520baf26b0SMartin KaFai Lau 
530baf26b0SMartin KaFai Lau static bool is_optional(u32 member_offset)
540baf26b0SMartin KaFai Lau {
550baf26b0SMartin KaFai Lau 	unsigned int i;
560baf26b0SMartin KaFai Lau 
570baf26b0SMartin KaFai Lau 	for (i = 0; i < ARRAY_SIZE(optional_ops); i++) {
580baf26b0SMartin KaFai Lau 		if (member_offset == optional_ops[i])
590baf26b0SMartin KaFai Lau 			return true;
600baf26b0SMartin KaFai Lau 	}
610baf26b0SMartin KaFai Lau 
620baf26b0SMartin KaFai Lau 	return false;
630baf26b0SMartin KaFai Lau }
640baf26b0SMartin KaFai Lau 
650baf26b0SMartin KaFai Lau static bool is_unsupported(u32 member_offset)
660baf26b0SMartin KaFai Lau {
670baf26b0SMartin KaFai Lau 	unsigned int i;
680baf26b0SMartin KaFai Lau 
690baf26b0SMartin KaFai Lau 	for (i = 0; i < ARRAY_SIZE(unsupported_ops); i++) {
700baf26b0SMartin KaFai Lau 		if (member_offset == unsupported_ops[i])
710baf26b0SMartin KaFai Lau 			return true;
720baf26b0SMartin KaFai Lau 	}
730baf26b0SMartin KaFai Lau 
740baf26b0SMartin KaFai Lau 	return false;
750baf26b0SMartin KaFai Lau }
760baf26b0SMartin KaFai Lau 
770baf26b0SMartin KaFai Lau extern struct btf *btf_vmlinux;
780baf26b0SMartin KaFai Lau 
790baf26b0SMartin KaFai Lau static bool bpf_tcp_ca_is_valid_access(int off, int size,
800baf26b0SMartin KaFai Lau 				       enum bpf_access_type type,
810baf26b0SMartin KaFai Lau 				       const struct bpf_prog *prog,
820baf26b0SMartin KaFai Lau 				       struct bpf_insn_access_aux *info)
830baf26b0SMartin KaFai Lau {
8435346ab6SHou Tao 	if (!bpf_tracing_btf_ctx_access(off, size, type, prog, info))
850baf26b0SMartin KaFai Lau 		return false;
860baf26b0SMartin KaFai Lau 
870baf26b0SMartin KaFai Lau 	if (info->reg_type == PTR_TO_BTF_ID && info->btf_id == sock_id)
880baf26b0SMartin KaFai Lau 		/* promote it to tcp_sock */
890baf26b0SMartin KaFai Lau 		info->btf_id = tcp_sock_id;
900baf26b0SMartin KaFai Lau 
910baf26b0SMartin KaFai Lau 	return true;
920baf26b0SMartin KaFai Lau }
930baf26b0SMartin KaFai Lau 
940baf26b0SMartin KaFai Lau static int bpf_tcp_ca_btf_struct_access(struct bpf_verifier_log *log,
9522dc4a0fSAndrii Nakryiko 					const struct btf *btf,
960baf26b0SMartin KaFai Lau 					const struct btf_type *t, int off,
970baf26b0SMartin KaFai Lau 					int size, enum bpf_access_type atype,
980baf26b0SMartin KaFai Lau 					u32 *next_btf_id)
990baf26b0SMartin KaFai Lau {
1000baf26b0SMartin KaFai Lau 	size_t end;
1010baf26b0SMartin KaFai Lau 
1020baf26b0SMartin KaFai Lau 	if (atype == BPF_READ)
10322dc4a0fSAndrii Nakryiko 		return btf_struct_access(log, btf, t, off, size, atype, next_btf_id);
1040baf26b0SMartin KaFai Lau 
1050baf26b0SMartin KaFai Lau 	if (t != tcp_sock_type) {
1060baf26b0SMartin KaFai Lau 		bpf_log(log, "only read is supported\n");
1070baf26b0SMartin KaFai Lau 		return -EACCES;
1080baf26b0SMartin KaFai Lau 	}
1090baf26b0SMartin KaFai Lau 
1100baf26b0SMartin KaFai Lau 	switch (off) {
1110baf26b0SMartin KaFai Lau 	case bpf_ctx_range(struct inet_connection_sock, icsk_ca_priv):
1120baf26b0SMartin KaFai Lau 		end = offsetofend(struct inet_connection_sock, icsk_ca_priv);
1130baf26b0SMartin KaFai Lau 		break;
1140baf26b0SMartin KaFai Lau 	case offsetof(struct inet_connection_sock, icsk_ack.pending):
1150baf26b0SMartin KaFai Lau 		end = offsetofend(struct inet_connection_sock,
1160baf26b0SMartin KaFai Lau 				  icsk_ack.pending);
1170baf26b0SMartin KaFai Lau 		break;
1180baf26b0SMartin KaFai Lau 	case offsetof(struct tcp_sock, snd_cwnd):
1190baf26b0SMartin KaFai Lau 		end = offsetofend(struct tcp_sock, snd_cwnd);
1200baf26b0SMartin KaFai Lau 		break;
1210baf26b0SMartin KaFai Lau 	case offsetof(struct tcp_sock, snd_cwnd_cnt):
1220baf26b0SMartin KaFai Lau 		end = offsetofend(struct tcp_sock, snd_cwnd_cnt);
1230baf26b0SMartin KaFai Lau 		break;
1240baf26b0SMartin KaFai Lau 	case offsetof(struct tcp_sock, snd_ssthresh):
1250baf26b0SMartin KaFai Lau 		end = offsetofend(struct tcp_sock, snd_ssthresh);
1260baf26b0SMartin KaFai Lau 		break;
1270baf26b0SMartin KaFai Lau 	case offsetof(struct tcp_sock, ecn_flags):
1280baf26b0SMartin KaFai Lau 		end = offsetofend(struct tcp_sock, ecn_flags);
1290baf26b0SMartin KaFai Lau 		break;
1300baf26b0SMartin KaFai Lau 	default:
1310baf26b0SMartin KaFai Lau 		bpf_log(log, "no write support to tcp_sock at off %d\n", off);
1320baf26b0SMartin KaFai Lau 		return -EACCES;
1330baf26b0SMartin KaFai Lau 	}
1340baf26b0SMartin KaFai Lau 
1350baf26b0SMartin KaFai Lau 	if (off + size > end) {
1360baf26b0SMartin KaFai Lau 		bpf_log(log,
1370baf26b0SMartin KaFai Lau 			"write access at off %d with size %d beyond the member of tcp_sock ended at %zu\n",
1380baf26b0SMartin KaFai Lau 			off, size, end);
1390baf26b0SMartin KaFai Lau 		return -EACCES;
1400baf26b0SMartin KaFai Lau 	}
1410baf26b0SMartin KaFai Lau 
1420baf26b0SMartin KaFai Lau 	return NOT_INIT;
1430baf26b0SMartin KaFai Lau }
1440baf26b0SMartin KaFai Lau 
145206057feSMartin KaFai Lau BPF_CALL_2(bpf_tcp_send_ack, struct tcp_sock *, tp, u32, rcv_nxt)
146206057feSMartin KaFai Lau {
147206057feSMartin KaFai Lau 	/* bpf_tcp_ca prog cannot have NULL tp */
148206057feSMartin KaFai Lau 	__tcp_send_ack((struct sock *)tp, rcv_nxt);
149206057feSMartin KaFai Lau 	return 0;
150206057feSMartin KaFai Lau }
151206057feSMartin KaFai Lau 
152206057feSMartin KaFai Lau static const struct bpf_func_proto bpf_tcp_send_ack_proto = {
153206057feSMartin KaFai Lau 	.func		= bpf_tcp_send_ack,
154206057feSMartin KaFai Lau 	.gpl_only	= false,
155206057feSMartin KaFai Lau 	/* In case we want to report error later */
156206057feSMartin KaFai Lau 	.ret_type	= RET_INTEGER,
157206057feSMartin KaFai Lau 	.arg1_type	= ARG_PTR_TO_BTF_ID,
1589436ef6eSLorenz Bauer 	.arg1_btf_id	= &tcp_sock_id,
159206057feSMartin KaFai Lau 	.arg2_type	= ARG_ANYTHING,
160206057feSMartin KaFai Lau };
161206057feSMartin KaFai Lau 
162eb18b49eSMartin KaFai Lau static u32 prog_ops_moff(const struct bpf_prog *prog)
163eb18b49eSMartin KaFai Lau {
164eb18b49eSMartin KaFai Lau 	const struct btf_member *m;
165eb18b49eSMartin KaFai Lau 	const struct btf_type *t;
166eb18b49eSMartin KaFai Lau 	u32 midx;
167eb18b49eSMartin KaFai Lau 
168eb18b49eSMartin KaFai Lau 	midx = prog->expected_attach_type;
169eb18b49eSMartin KaFai Lau 	t = bpf_tcp_congestion_ops.type;
170eb18b49eSMartin KaFai Lau 	m = &btf_type_member(t)[midx];
171eb18b49eSMartin KaFai Lau 
172eb18b49eSMartin KaFai Lau 	return btf_member_bit_offset(t, m) / 8;
173eb18b49eSMartin KaFai Lau }
174eb18b49eSMartin KaFai Lau 
1750baf26b0SMartin KaFai Lau static const struct bpf_func_proto *
1760baf26b0SMartin KaFai Lau bpf_tcp_ca_get_func_proto(enum bpf_func_id func_id,
1770baf26b0SMartin KaFai Lau 			  const struct bpf_prog *prog)
1780baf26b0SMartin KaFai Lau {
179206057feSMartin KaFai Lau 	switch (func_id) {
180206057feSMartin KaFai Lau 	case BPF_FUNC_tcp_send_ack:
181206057feSMartin KaFai Lau 		return &bpf_tcp_send_ack_proto;
182ab14fd4eSMartin KaFai Lau 	case BPF_FUNC_sk_storage_get:
183592a3498SMartin KaFai Lau 		return &bpf_sk_storage_get_proto;
184ab14fd4eSMartin KaFai Lau 	case BPF_FUNC_sk_storage_delete:
185592a3498SMartin KaFai Lau 		return &bpf_sk_storage_delete_proto;
186eb18b49eSMartin KaFai Lau 	case BPF_FUNC_setsockopt:
187eb18b49eSMartin KaFai Lau 		/* Does not allow release() to call setsockopt.
188eb18b49eSMartin KaFai Lau 		 * release() is called when the current bpf-tcp-cc
189eb18b49eSMartin KaFai Lau 		 * is retiring.  It is not allowed to call
190eb18b49eSMartin KaFai Lau 		 * setsockopt() to make further changes which
191eb18b49eSMartin KaFai Lau 		 * may potentially allocate new resources.
192eb18b49eSMartin KaFai Lau 		 */
193eb18b49eSMartin KaFai Lau 		if (prog_ops_moff(prog) !=
194eb18b49eSMartin KaFai Lau 		    offsetof(struct tcp_congestion_ops, release))
195eb18b49eSMartin KaFai Lau 			return &bpf_sk_setsockopt_proto;
196eb18b49eSMartin KaFai Lau 		return NULL;
197eb18b49eSMartin KaFai Lau 	case BPF_FUNC_getsockopt:
198eb18b49eSMartin KaFai Lau 		/* Since get/setsockopt is usually expected to
199eb18b49eSMartin KaFai Lau 		 * be available together, disable getsockopt for
200eb18b49eSMartin KaFai Lau 		 * release also to avoid usage surprise.
201eb18b49eSMartin KaFai Lau 		 * The bpf-tcp-cc already has a more powerful way
202eb18b49eSMartin KaFai Lau 		 * to read tcp_sock from the PTR_TO_BTF_ID.
203eb18b49eSMartin KaFai Lau 		 */
204eb18b49eSMartin KaFai Lau 		if (prog_ops_moff(prog) !=
205eb18b49eSMartin KaFai Lau 		    offsetof(struct tcp_congestion_ops, release))
206eb18b49eSMartin KaFai Lau 			return &bpf_sk_getsockopt_proto;
207eb18b49eSMartin KaFai Lau 		return NULL;
208*5e0bc308SDmitrii Banshchikov 	case BPF_FUNC_ktime_get_coarse_ns:
209*5e0bc308SDmitrii Banshchikov 		return &bpf_ktime_get_coarse_ns_proto;
210206057feSMartin KaFai Lau 	default:
2110baf26b0SMartin KaFai Lau 		return bpf_base_func_proto(func_id);
2120baf26b0SMartin KaFai Lau 	}
213206057feSMartin KaFai Lau }
2140baf26b0SMartin KaFai Lau 
215e78aea8bSMartin KaFai Lau BTF_SET_START(bpf_tcp_ca_kfunc_ids)
216e78aea8bSMartin KaFai Lau BTF_ID(func, tcp_reno_ssthresh)
217e78aea8bSMartin KaFai Lau BTF_ID(func, tcp_reno_cong_avoid)
218e78aea8bSMartin KaFai Lau BTF_ID(func, tcp_reno_undo_cwnd)
219e78aea8bSMartin KaFai Lau BTF_ID(func, tcp_slow_start)
220e78aea8bSMartin KaFai Lau BTF_ID(func, tcp_cong_avoid_ai)
221e78aea8bSMartin KaFai Lau BTF_SET_END(bpf_tcp_ca_kfunc_ids)
222e78aea8bSMartin KaFai Lau 
2232357672cSKumar Kartikeya Dwivedi static bool bpf_tcp_ca_check_kfunc_call(u32 kfunc_btf_id, struct module *owner)
224e78aea8bSMartin KaFai Lau {
2250e32dfc8SKumar Kartikeya Dwivedi 	if (btf_id_set_contains(&bpf_tcp_ca_kfunc_ids, kfunc_btf_id))
2260e32dfc8SKumar Kartikeya Dwivedi 		return true;
2270e32dfc8SKumar Kartikeya Dwivedi 	return bpf_check_mod_kfunc_call(&bpf_tcp_ca_kfunc_list, kfunc_btf_id, owner);
228e78aea8bSMartin KaFai Lau }
229e78aea8bSMartin KaFai Lau 
2300baf26b0SMartin KaFai Lau static const struct bpf_verifier_ops bpf_tcp_ca_verifier_ops = {
2310baf26b0SMartin KaFai Lau 	.get_func_proto		= bpf_tcp_ca_get_func_proto,
2320baf26b0SMartin KaFai Lau 	.is_valid_access	= bpf_tcp_ca_is_valid_access,
2330baf26b0SMartin KaFai Lau 	.btf_struct_access	= bpf_tcp_ca_btf_struct_access,
234e78aea8bSMartin KaFai Lau 	.check_kfunc_call	= bpf_tcp_ca_check_kfunc_call,
2350baf26b0SMartin KaFai Lau };
2360baf26b0SMartin KaFai Lau 
2370baf26b0SMartin KaFai Lau static int bpf_tcp_ca_init_member(const struct btf_type *t,
2380baf26b0SMartin KaFai Lau 				  const struct btf_member *member,
2390baf26b0SMartin KaFai Lau 				  void *kdata, const void *udata)
2400baf26b0SMartin KaFai Lau {
2410baf26b0SMartin KaFai Lau 	const struct tcp_congestion_ops *utcp_ca;
2420baf26b0SMartin KaFai Lau 	struct tcp_congestion_ops *tcp_ca;
2430baf26b0SMartin KaFai Lau 	int prog_fd;
2440baf26b0SMartin KaFai Lau 	u32 moff;
2450baf26b0SMartin KaFai Lau 
2460baf26b0SMartin KaFai Lau 	utcp_ca = (const struct tcp_congestion_ops *)udata;
2470baf26b0SMartin KaFai Lau 	tcp_ca = (struct tcp_congestion_ops *)kdata;
2480baf26b0SMartin KaFai Lau 
2490baf26b0SMartin KaFai Lau 	moff = btf_member_bit_offset(t, member) / 8;
2500baf26b0SMartin KaFai Lau 	switch (moff) {
2510baf26b0SMartin KaFai Lau 	case offsetof(struct tcp_congestion_ops, flags):
2520baf26b0SMartin KaFai Lau 		if (utcp_ca->flags & ~TCP_CONG_MASK)
2530baf26b0SMartin KaFai Lau 			return -EINVAL;
2540baf26b0SMartin KaFai Lau 		tcp_ca->flags = utcp_ca->flags;
2550baf26b0SMartin KaFai Lau 		return 1;
2560baf26b0SMartin KaFai Lau 	case offsetof(struct tcp_congestion_ops, name):
2578e7ae251SMartin KaFai Lau 		if (bpf_obj_name_cpy(tcp_ca->name, utcp_ca->name,
2588e7ae251SMartin KaFai Lau 				     sizeof(tcp_ca->name)) <= 0)
2590baf26b0SMartin KaFai Lau 			return -EINVAL;
2600baf26b0SMartin KaFai Lau 		if (tcp_ca_find(utcp_ca->name))
2610baf26b0SMartin KaFai Lau 			return -EEXIST;
2620baf26b0SMartin KaFai Lau 		return 1;
2630baf26b0SMartin KaFai Lau 	}
2640baf26b0SMartin KaFai Lau 
2650baf26b0SMartin KaFai Lau 	if (!btf_type_resolve_func_ptr(btf_vmlinux, member->type, NULL))
2660baf26b0SMartin KaFai Lau 		return 0;
2670baf26b0SMartin KaFai Lau 
2680baf26b0SMartin KaFai Lau 	/* Ensure bpf_prog is provided for compulsory func ptr */
2690baf26b0SMartin KaFai Lau 	prog_fd = (int)(*(unsigned long *)(udata + moff));
2700baf26b0SMartin KaFai Lau 	if (!prog_fd && !is_optional(moff) && !is_unsupported(moff))
2710baf26b0SMartin KaFai Lau 		return -EINVAL;
2720baf26b0SMartin KaFai Lau 
2730baf26b0SMartin KaFai Lau 	return 0;
2740baf26b0SMartin KaFai Lau }
2750baf26b0SMartin KaFai Lau 
2760baf26b0SMartin KaFai Lau static int bpf_tcp_ca_check_member(const struct btf_type *t,
2770baf26b0SMartin KaFai Lau 				   const struct btf_member *member)
2780baf26b0SMartin KaFai Lau {
2790baf26b0SMartin KaFai Lau 	if (is_unsupported(btf_member_bit_offset(t, member) / 8))
2800baf26b0SMartin KaFai Lau 		return -ENOTSUPP;
2810baf26b0SMartin KaFai Lau 	return 0;
2820baf26b0SMartin KaFai Lau }
2830baf26b0SMartin KaFai Lau 
2840baf26b0SMartin KaFai Lau static int bpf_tcp_ca_reg(void *kdata)
2850baf26b0SMartin KaFai Lau {
2860baf26b0SMartin KaFai Lau 	return tcp_register_congestion_control(kdata);
2870baf26b0SMartin KaFai Lau }
2880baf26b0SMartin KaFai Lau 
2890baf26b0SMartin KaFai Lau static void bpf_tcp_ca_unreg(void *kdata)
2900baf26b0SMartin KaFai Lau {
2910baf26b0SMartin KaFai Lau 	tcp_unregister_congestion_control(kdata);
2920baf26b0SMartin KaFai Lau }
2930baf26b0SMartin KaFai Lau 
2940baf26b0SMartin KaFai Lau struct bpf_struct_ops bpf_tcp_congestion_ops = {
2950baf26b0SMartin KaFai Lau 	.verifier_ops = &bpf_tcp_ca_verifier_ops,
2960baf26b0SMartin KaFai Lau 	.reg = bpf_tcp_ca_reg,
2970baf26b0SMartin KaFai Lau 	.unreg = bpf_tcp_ca_unreg,
2980baf26b0SMartin KaFai Lau 	.check_member = bpf_tcp_ca_check_member,
2990baf26b0SMartin KaFai Lau 	.init_member = bpf_tcp_ca_init_member,
3000baf26b0SMartin KaFai Lau 	.init = bpf_tcp_ca_init,
3010baf26b0SMartin KaFai Lau 	.name = "tcp_congestion_ops",
3020baf26b0SMartin KaFai Lau };
303