xref: /linux/net/ipv4/bpf_tcp_ca.c (revision 51a52a29ebaa8395de090fa415c6e1b2899a50f1)
10baf26b0SMartin KaFai Lau // SPDX-License-Identifier: GPL-2.0
20baf26b0SMartin KaFai Lau /* Copyright (c) 2019 Facebook  */
30baf26b0SMartin KaFai Lau 
4b202d844SKumar Kartikeya Dwivedi #include <linux/init.h>
50baf26b0SMartin KaFai Lau #include <linux/types.h>
60baf26b0SMartin KaFai Lau #include <linux/bpf_verifier.h>
70baf26b0SMartin KaFai Lau #include <linux/bpf.h>
80baf26b0SMartin KaFai Lau #include <linux/btf.h>
9e78aea8bSMartin KaFai Lau #include <linux/btf_ids.h>
100baf26b0SMartin KaFai Lau #include <linux/filter.h>
110baf26b0SMartin KaFai Lau #include <net/tcp.h>
12ab14fd4eSMartin KaFai Lau #include <net/bpf_sk_storage.h>
130baf26b0SMartin KaFai Lau 
14eb18b49eSMartin KaFai Lau /* "extern" is to avoid sparse warning.  It is only used in bpf_struct_ops.c. */
15eb18b49eSMartin KaFai Lau extern struct bpf_struct_ops bpf_tcp_congestion_ops;
16eb18b49eSMartin KaFai Lau 
170baf26b0SMartin KaFai Lau static u32 unsupported_ops[] = {
180baf26b0SMartin KaFai Lau 	offsetof(struct tcp_congestion_ops, get_info),
190baf26b0SMartin KaFai Lau };
200baf26b0SMartin KaFai Lau 
210baf26b0SMartin KaFai Lau static const struct btf_type *tcp_sock_type;
220baf26b0SMartin KaFai Lau static u32 tcp_sock_id, sock_id;
230baf26b0SMartin KaFai Lau 
240baf26b0SMartin KaFai Lau static int bpf_tcp_ca_init(struct btf *btf)
250baf26b0SMartin KaFai Lau {
260baf26b0SMartin KaFai Lau 	s32 type_id;
270baf26b0SMartin KaFai Lau 
280baf26b0SMartin KaFai Lau 	type_id = btf_find_by_name_kind(btf, "sock", BTF_KIND_STRUCT);
290baf26b0SMartin KaFai Lau 	if (type_id < 0)
300baf26b0SMartin KaFai Lau 		return -EINVAL;
310baf26b0SMartin KaFai Lau 	sock_id = type_id;
320baf26b0SMartin KaFai Lau 
330baf26b0SMartin KaFai Lau 	type_id = btf_find_by_name_kind(btf, "tcp_sock", BTF_KIND_STRUCT);
340baf26b0SMartin KaFai Lau 	if (type_id < 0)
350baf26b0SMartin KaFai Lau 		return -EINVAL;
360baf26b0SMartin KaFai Lau 	tcp_sock_id = type_id;
370baf26b0SMartin KaFai Lau 	tcp_sock_type = btf_type_by_id(btf, tcp_sock_id);
380baf26b0SMartin KaFai Lau 
390baf26b0SMartin KaFai Lau 	return 0;
400baf26b0SMartin KaFai Lau }
410baf26b0SMartin KaFai Lau 
420baf26b0SMartin KaFai Lau static bool is_unsupported(u32 member_offset)
430baf26b0SMartin KaFai Lau {
440baf26b0SMartin KaFai Lau 	unsigned int i;
450baf26b0SMartin KaFai Lau 
460baf26b0SMartin KaFai Lau 	for (i = 0; i < ARRAY_SIZE(unsupported_ops); i++) {
470baf26b0SMartin KaFai Lau 		if (member_offset == unsupported_ops[i])
480baf26b0SMartin KaFai Lau 			return true;
490baf26b0SMartin KaFai Lau 	}
500baf26b0SMartin KaFai Lau 
510baf26b0SMartin KaFai Lau 	return false;
520baf26b0SMartin KaFai Lau }
530baf26b0SMartin KaFai Lau 
540baf26b0SMartin KaFai Lau extern struct btf *btf_vmlinux;
550baf26b0SMartin KaFai Lau 
560baf26b0SMartin KaFai Lau static bool bpf_tcp_ca_is_valid_access(int off, int size,
570baf26b0SMartin KaFai Lau 				       enum bpf_access_type type,
580baf26b0SMartin KaFai Lau 				       const struct bpf_prog *prog,
590baf26b0SMartin KaFai Lau 				       struct bpf_insn_access_aux *info)
600baf26b0SMartin KaFai Lau {
6135346ab6SHou Tao 	if (!bpf_tracing_btf_ctx_access(off, size, type, prog, info))
620baf26b0SMartin KaFai Lau 		return false;
630baf26b0SMartin KaFai Lau 
643f00c523SDavid Vernet 	if (base_type(info->reg_type) == PTR_TO_BTF_ID &&
653f00c523SDavid Vernet 	    !bpf_type_has_unsafe_modifiers(info->reg_type) &&
663f00c523SDavid Vernet 	    info->btf_id == sock_id)
670baf26b0SMartin KaFai Lau 		/* promote it to tcp_sock */
680baf26b0SMartin KaFai Lau 		info->btf_id = tcp_sock_id;
690baf26b0SMartin KaFai Lau 
700baf26b0SMartin KaFai Lau 	return true;
710baf26b0SMartin KaFai Lau }
720baf26b0SMartin KaFai Lau 
730baf26b0SMartin KaFai Lau static int bpf_tcp_ca_btf_struct_access(struct bpf_verifier_log *log,
746728aea7SKumar Kartikeya Dwivedi 					const struct bpf_reg_state *reg,
756728aea7SKumar Kartikeya Dwivedi 					int off, int size, enum bpf_access_type atype,
766728aea7SKumar Kartikeya Dwivedi 					u32 *next_btf_id, enum bpf_type_flag *flag)
770baf26b0SMartin KaFai Lau {
786728aea7SKumar Kartikeya Dwivedi 	const struct btf_type *t;
790baf26b0SMartin KaFai Lau 	size_t end;
800baf26b0SMartin KaFai Lau 
810baf26b0SMartin KaFai Lau 	if (atype == BPF_READ)
826728aea7SKumar Kartikeya Dwivedi 		return btf_struct_access(log, reg, off, size, atype, next_btf_id, flag);
830baf26b0SMartin KaFai Lau 
846728aea7SKumar Kartikeya Dwivedi 	t = btf_type_by_id(reg->btf, reg->btf_id);
850baf26b0SMartin KaFai Lau 	if (t != tcp_sock_type) {
860baf26b0SMartin KaFai Lau 		bpf_log(log, "only read is supported\n");
870baf26b0SMartin KaFai Lau 		return -EACCES;
880baf26b0SMartin KaFai Lau 	}
890baf26b0SMartin KaFai Lau 
900baf26b0SMartin KaFai Lau 	switch (off) {
9141c95dd6SJörn-Thorben Hinz 	case offsetof(struct sock, sk_pacing_rate):
9241c95dd6SJörn-Thorben Hinz 		end = offsetofend(struct sock, sk_pacing_rate);
9341c95dd6SJörn-Thorben Hinz 		break;
9441c95dd6SJörn-Thorben Hinz 	case offsetof(struct sock, sk_pacing_status):
9541c95dd6SJörn-Thorben Hinz 		end = offsetofend(struct sock, sk_pacing_status);
9641c95dd6SJörn-Thorben Hinz 		break;
970baf26b0SMartin KaFai Lau 	case bpf_ctx_range(struct inet_connection_sock, icsk_ca_priv):
980baf26b0SMartin KaFai Lau 		end = offsetofend(struct inet_connection_sock, icsk_ca_priv);
990baf26b0SMartin KaFai Lau 		break;
1000baf26b0SMartin KaFai Lau 	case offsetof(struct inet_connection_sock, icsk_ack.pending):
1010baf26b0SMartin KaFai Lau 		end = offsetofend(struct inet_connection_sock,
1020baf26b0SMartin KaFai Lau 				  icsk_ack.pending);
1030baf26b0SMartin KaFai Lau 		break;
1040baf26b0SMartin KaFai Lau 	case offsetof(struct tcp_sock, snd_cwnd):
1050baf26b0SMartin KaFai Lau 		end = offsetofend(struct tcp_sock, snd_cwnd);
1060baf26b0SMartin KaFai Lau 		break;
1070baf26b0SMartin KaFai Lau 	case offsetof(struct tcp_sock, snd_cwnd_cnt):
1080baf26b0SMartin KaFai Lau 		end = offsetofend(struct tcp_sock, snd_cwnd_cnt);
1090baf26b0SMartin KaFai Lau 		break;
1100baf26b0SMartin KaFai Lau 	case offsetof(struct tcp_sock, snd_ssthresh):
1110baf26b0SMartin KaFai Lau 		end = offsetofend(struct tcp_sock, snd_ssthresh);
1120baf26b0SMartin KaFai Lau 		break;
1130baf26b0SMartin KaFai Lau 	case offsetof(struct tcp_sock, ecn_flags):
1140baf26b0SMartin KaFai Lau 		end = offsetofend(struct tcp_sock, ecn_flags);
1150baf26b0SMartin KaFai Lau 		break;
1160baf26b0SMartin KaFai Lau 	default:
1170baf26b0SMartin KaFai Lau 		bpf_log(log, "no write support to tcp_sock at off %d\n", off);
1180baf26b0SMartin KaFai Lau 		return -EACCES;
1190baf26b0SMartin KaFai Lau 	}
1200baf26b0SMartin KaFai Lau 
1210baf26b0SMartin KaFai Lau 	if (off + size > end) {
1220baf26b0SMartin KaFai Lau 		bpf_log(log,
1230baf26b0SMartin KaFai Lau 			"write access at off %d with size %d beyond the member of tcp_sock ended at %zu\n",
1240baf26b0SMartin KaFai Lau 			off, size, end);
1250baf26b0SMartin KaFai Lau 		return -EACCES;
1260baf26b0SMartin KaFai Lau 	}
1270baf26b0SMartin KaFai Lau 
128896f07c0SDaniel Xu 	return 0;
1290baf26b0SMartin KaFai Lau }
1300baf26b0SMartin KaFai Lau 
131206057feSMartin KaFai Lau BPF_CALL_2(bpf_tcp_send_ack, struct tcp_sock *, tp, u32, rcv_nxt)
132206057feSMartin KaFai Lau {
133206057feSMartin KaFai Lau 	/* bpf_tcp_ca prog cannot have NULL tp */
134206057feSMartin KaFai Lau 	__tcp_send_ack((struct sock *)tp, rcv_nxt);
135206057feSMartin KaFai Lau 	return 0;
136206057feSMartin KaFai Lau }
137206057feSMartin KaFai Lau 
138206057feSMartin KaFai Lau static const struct bpf_func_proto bpf_tcp_send_ack_proto = {
139206057feSMartin KaFai Lau 	.func		= bpf_tcp_send_ack,
140206057feSMartin KaFai Lau 	.gpl_only	= false,
141206057feSMartin KaFai Lau 	/* In case we want to report error later */
142206057feSMartin KaFai Lau 	.ret_type	= RET_INTEGER,
143206057feSMartin KaFai Lau 	.arg1_type	= ARG_PTR_TO_BTF_ID,
1449436ef6eSLorenz Bauer 	.arg1_btf_id	= &tcp_sock_id,
145206057feSMartin KaFai Lau 	.arg2_type	= ARG_ANYTHING,
146206057feSMartin KaFai Lau };
147206057feSMartin KaFai Lau 
148eb18b49eSMartin KaFai Lau static u32 prog_ops_moff(const struct bpf_prog *prog)
149eb18b49eSMartin KaFai Lau {
150eb18b49eSMartin KaFai Lau 	const struct btf_member *m;
151eb18b49eSMartin KaFai Lau 	const struct btf_type *t;
152eb18b49eSMartin KaFai Lau 	u32 midx;
153eb18b49eSMartin KaFai Lau 
154eb18b49eSMartin KaFai Lau 	midx = prog->expected_attach_type;
155eb18b49eSMartin KaFai Lau 	t = bpf_tcp_congestion_ops.type;
156eb18b49eSMartin KaFai Lau 	m = &btf_type_member(t)[midx];
157eb18b49eSMartin KaFai Lau 
1588293eb99SAlexei Starovoitov 	return __btf_member_bit_offset(t, m) / 8;
159eb18b49eSMartin KaFai Lau }
160eb18b49eSMartin KaFai Lau 
1610baf26b0SMartin KaFai Lau static const struct bpf_func_proto *
1620baf26b0SMartin KaFai Lau bpf_tcp_ca_get_func_proto(enum bpf_func_id func_id,
1630baf26b0SMartin KaFai Lau 			  const struct bpf_prog *prog)
1640baf26b0SMartin KaFai Lau {
165206057feSMartin KaFai Lau 	switch (func_id) {
166206057feSMartin KaFai Lau 	case BPF_FUNC_tcp_send_ack:
167206057feSMartin KaFai Lau 		return &bpf_tcp_send_ack_proto;
168ab14fd4eSMartin KaFai Lau 	case BPF_FUNC_sk_storage_get:
169592a3498SMartin KaFai Lau 		return &bpf_sk_storage_get_proto;
170ab14fd4eSMartin KaFai Lau 	case BPF_FUNC_sk_storage_delete:
171592a3498SMartin KaFai Lau 		return &bpf_sk_storage_delete_proto;
172eb18b49eSMartin KaFai Lau 	case BPF_FUNC_setsockopt:
173eb18b49eSMartin KaFai Lau 		/* Does not allow release() to call setsockopt.
174eb18b49eSMartin KaFai Lau 		 * release() is called when the current bpf-tcp-cc
175eb18b49eSMartin KaFai Lau 		 * is retiring.  It is not allowed to call
176eb18b49eSMartin KaFai Lau 		 * setsockopt() to make further changes which
177eb18b49eSMartin KaFai Lau 		 * may potentially allocate new resources.
178eb18b49eSMartin KaFai Lau 		 */
179eb18b49eSMartin KaFai Lau 		if (prog_ops_moff(prog) !=
180eb18b49eSMartin KaFai Lau 		    offsetof(struct tcp_congestion_ops, release))
181eb18b49eSMartin KaFai Lau 			return &bpf_sk_setsockopt_proto;
182eb18b49eSMartin KaFai Lau 		return NULL;
183eb18b49eSMartin KaFai Lau 	case BPF_FUNC_getsockopt:
184eb18b49eSMartin KaFai Lau 		/* Since get/setsockopt is usually expected to
185eb18b49eSMartin KaFai Lau 		 * be available together, disable getsockopt for
186eb18b49eSMartin KaFai Lau 		 * release also to avoid usage surprise.
187eb18b49eSMartin KaFai Lau 		 * The bpf-tcp-cc already has a more powerful way
188eb18b49eSMartin KaFai Lau 		 * to read tcp_sock from the PTR_TO_BTF_ID.
189eb18b49eSMartin KaFai Lau 		 */
190eb18b49eSMartin KaFai Lau 		if (prog_ops_moff(prog) !=
191eb18b49eSMartin KaFai Lau 		    offsetof(struct tcp_congestion_ops, release))
192eb18b49eSMartin KaFai Lau 			return &bpf_sk_getsockopt_proto;
193eb18b49eSMartin KaFai Lau 		return NULL;
1945e0bc308SDmitrii Banshchikov 	case BPF_FUNC_ktime_get_coarse_ns:
1955e0bc308SDmitrii Banshchikov 		return &bpf_ktime_get_coarse_ns_proto;
196206057feSMartin KaFai Lau 	default:
1970baf26b0SMartin KaFai Lau 		return bpf_base_func_proto(func_id);
1980baf26b0SMartin KaFai Lau 	}
199206057feSMartin KaFai Lau }
2000baf26b0SMartin KaFai Lau 
201a4703e31SKumar Kartikeya Dwivedi BTF_SET8_START(bpf_tcp_ca_check_kfunc_ids)
202a4703e31SKumar Kartikeya Dwivedi BTF_ID_FLAGS(func, tcp_reno_ssthresh)
203a4703e31SKumar Kartikeya Dwivedi BTF_ID_FLAGS(func, tcp_reno_cong_avoid)
204a4703e31SKumar Kartikeya Dwivedi BTF_ID_FLAGS(func, tcp_reno_undo_cwnd)
205a4703e31SKumar Kartikeya Dwivedi BTF_ID_FLAGS(func, tcp_slow_start)
206a4703e31SKumar Kartikeya Dwivedi BTF_ID_FLAGS(func, tcp_cong_avoid_ai)
207a4703e31SKumar Kartikeya Dwivedi BTF_SET8_END(bpf_tcp_ca_check_kfunc_ids)
208e78aea8bSMartin KaFai Lau 
209b202d844SKumar Kartikeya Dwivedi static const struct btf_kfunc_id_set bpf_tcp_ca_kfunc_set = {
210b202d844SKumar Kartikeya Dwivedi 	.owner = THIS_MODULE,
211a4703e31SKumar Kartikeya Dwivedi 	.set   = &bpf_tcp_ca_check_kfunc_ids,
212b202d844SKumar Kartikeya Dwivedi };
213e78aea8bSMartin KaFai Lau 
2140baf26b0SMartin KaFai Lau static const struct bpf_verifier_ops bpf_tcp_ca_verifier_ops = {
2150baf26b0SMartin KaFai Lau 	.get_func_proto		= bpf_tcp_ca_get_func_proto,
2160baf26b0SMartin KaFai Lau 	.is_valid_access	= bpf_tcp_ca_is_valid_access,
2170baf26b0SMartin KaFai Lau 	.btf_struct_access	= bpf_tcp_ca_btf_struct_access,
2180baf26b0SMartin KaFai Lau };
2190baf26b0SMartin KaFai Lau 
2200baf26b0SMartin KaFai Lau static int bpf_tcp_ca_init_member(const struct btf_type *t,
2210baf26b0SMartin KaFai Lau 				  const struct btf_member *member,
2220baf26b0SMartin KaFai Lau 				  void *kdata, const void *udata)
2230baf26b0SMartin KaFai Lau {
2240baf26b0SMartin KaFai Lau 	const struct tcp_congestion_ops *utcp_ca;
2250baf26b0SMartin KaFai Lau 	struct tcp_congestion_ops *tcp_ca;
2260baf26b0SMartin KaFai Lau 	u32 moff;
2270baf26b0SMartin KaFai Lau 
2280baf26b0SMartin KaFai Lau 	utcp_ca = (const struct tcp_congestion_ops *)udata;
2290baf26b0SMartin KaFai Lau 	tcp_ca = (struct tcp_congestion_ops *)kdata;
2300baf26b0SMartin KaFai Lau 
2318293eb99SAlexei Starovoitov 	moff = __btf_member_bit_offset(t, member) / 8;
2320baf26b0SMartin KaFai Lau 	switch (moff) {
2330baf26b0SMartin KaFai Lau 	case offsetof(struct tcp_congestion_ops, flags):
2340baf26b0SMartin KaFai Lau 		if (utcp_ca->flags & ~TCP_CONG_MASK)
2350baf26b0SMartin KaFai Lau 			return -EINVAL;
2360baf26b0SMartin KaFai Lau 		tcp_ca->flags = utcp_ca->flags;
2370baf26b0SMartin KaFai Lau 		return 1;
2380baf26b0SMartin KaFai Lau 	case offsetof(struct tcp_congestion_ops, name):
2398e7ae251SMartin KaFai Lau 		if (bpf_obj_name_cpy(tcp_ca->name, utcp_ca->name,
2408e7ae251SMartin KaFai Lau 				     sizeof(tcp_ca->name)) <= 0)
2410baf26b0SMartin KaFai Lau 			return -EINVAL;
2420baf26b0SMartin KaFai Lau 		if (tcp_ca_find(utcp_ca->name))
2430baf26b0SMartin KaFai Lau 			return -EEXIST;
2440baf26b0SMartin KaFai Lau 		return 1;
2450baf26b0SMartin KaFai Lau 	}
2460baf26b0SMartin KaFai Lau 
2470baf26b0SMartin KaFai Lau 	return 0;
2480baf26b0SMartin KaFai Lau }
2490baf26b0SMartin KaFai Lau 
2500baf26b0SMartin KaFai Lau static int bpf_tcp_ca_check_member(const struct btf_type *t,
251*51a52a29SDavid Vernet 				   const struct btf_member *member,
252*51a52a29SDavid Vernet 				   const struct bpf_prog *prog)
2530baf26b0SMartin KaFai Lau {
2548293eb99SAlexei Starovoitov 	if (is_unsupported(__btf_member_bit_offset(t, member) / 8))
2550baf26b0SMartin KaFai Lau 		return -ENOTSUPP;
2560baf26b0SMartin KaFai Lau 	return 0;
2570baf26b0SMartin KaFai Lau }
2580baf26b0SMartin KaFai Lau 
2590baf26b0SMartin KaFai Lau static int bpf_tcp_ca_reg(void *kdata)
2600baf26b0SMartin KaFai Lau {
2610baf26b0SMartin KaFai Lau 	return tcp_register_congestion_control(kdata);
2620baf26b0SMartin KaFai Lau }
2630baf26b0SMartin KaFai Lau 
2640baf26b0SMartin KaFai Lau static void bpf_tcp_ca_unreg(void *kdata)
2650baf26b0SMartin KaFai Lau {
2660baf26b0SMartin KaFai Lau 	tcp_unregister_congestion_control(kdata);
2670baf26b0SMartin KaFai Lau }
2680baf26b0SMartin KaFai Lau 
2690baf26b0SMartin KaFai Lau struct bpf_struct_ops bpf_tcp_congestion_ops = {
2700baf26b0SMartin KaFai Lau 	.verifier_ops = &bpf_tcp_ca_verifier_ops,
2710baf26b0SMartin KaFai Lau 	.reg = bpf_tcp_ca_reg,
2720baf26b0SMartin KaFai Lau 	.unreg = bpf_tcp_ca_unreg,
2730baf26b0SMartin KaFai Lau 	.check_member = bpf_tcp_ca_check_member,
2740baf26b0SMartin KaFai Lau 	.init_member = bpf_tcp_ca_init_member,
2750baf26b0SMartin KaFai Lau 	.init = bpf_tcp_ca_init,
2760baf26b0SMartin KaFai Lau 	.name = "tcp_congestion_ops",
2770baf26b0SMartin KaFai Lau };
278b202d844SKumar Kartikeya Dwivedi 
279b202d844SKumar Kartikeya Dwivedi static int __init bpf_tcp_ca_kfunc_init(void)
280b202d844SKumar Kartikeya Dwivedi {
281b202d844SKumar Kartikeya Dwivedi 	return register_btf_kfunc_id_set(BPF_PROG_TYPE_STRUCT_OPS, &bpf_tcp_ca_kfunc_set);
282b202d844SKumar Kartikeya Dwivedi }
283b202d844SKumar Kartikeya Dwivedi late_initcall(bpf_tcp_ca_kfunc_init);
284