xref: /openbmc/linux/net/ipv4/bpf_tcp_ca.c (revision c900529f3d9161bfde5cca0754f83b4d3c3e0220)
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 
bpf_tcp_ca_init(struct btf * btf)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 
is_unsupported(u32 member_offset)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 
bpf_tcp_ca_is_valid_access(int off,int size,enum bpf_access_type type,const struct bpf_prog * prog,struct bpf_insn_access_aux * info)540baf26b0SMartin KaFai Lau static bool bpf_tcp_ca_is_valid_access(int off, int size,
550baf26b0SMartin KaFai Lau 				       enum bpf_access_type type,
560baf26b0SMartin KaFai Lau 				       const struct bpf_prog *prog,
570baf26b0SMartin KaFai Lau 				       struct bpf_insn_access_aux *info)
580baf26b0SMartin KaFai Lau {
5935346ab6SHou Tao 	if (!bpf_tracing_btf_ctx_access(off, size, type, prog, info))
600baf26b0SMartin KaFai Lau 		return false;
610baf26b0SMartin KaFai Lau 
623f00c523SDavid Vernet 	if (base_type(info->reg_type) == PTR_TO_BTF_ID &&
633f00c523SDavid Vernet 	    !bpf_type_has_unsafe_modifiers(info->reg_type) &&
643f00c523SDavid Vernet 	    info->btf_id == sock_id)
650baf26b0SMartin KaFai Lau 		/* promote it to tcp_sock */
660baf26b0SMartin KaFai Lau 		info->btf_id = tcp_sock_id;
670baf26b0SMartin KaFai Lau 
680baf26b0SMartin KaFai Lau 	return true;
690baf26b0SMartin KaFai Lau }
700baf26b0SMartin KaFai Lau 
bpf_tcp_ca_btf_struct_access(struct bpf_verifier_log * log,const struct bpf_reg_state * reg,int off,int size)710baf26b0SMartin KaFai Lau static int bpf_tcp_ca_btf_struct_access(struct bpf_verifier_log *log,
726728aea7SKumar Kartikeya Dwivedi 					const struct bpf_reg_state *reg,
73*b7e852a9SAlexei Starovoitov 					int off, int size)
740baf26b0SMartin KaFai Lau {
756728aea7SKumar Kartikeya Dwivedi 	const struct btf_type *t;
760baf26b0SMartin KaFai Lau 	size_t end;
770baf26b0SMartin KaFai Lau 
786728aea7SKumar Kartikeya Dwivedi 	t = btf_type_by_id(reg->btf, reg->btf_id);
790baf26b0SMartin KaFai Lau 	if (t != tcp_sock_type) {
800baf26b0SMartin KaFai Lau 		bpf_log(log, "only read is supported\n");
810baf26b0SMartin KaFai Lau 		return -EACCES;
820baf26b0SMartin KaFai Lau 	}
830baf26b0SMartin KaFai Lau 
840baf26b0SMartin KaFai Lau 	switch (off) {
8541c95dd6SJörn-Thorben Hinz 	case offsetof(struct sock, sk_pacing_rate):
8641c95dd6SJörn-Thorben Hinz 		end = offsetofend(struct sock, sk_pacing_rate);
8741c95dd6SJörn-Thorben Hinz 		break;
8841c95dd6SJörn-Thorben Hinz 	case offsetof(struct sock, sk_pacing_status):
8941c95dd6SJörn-Thorben Hinz 		end = offsetofend(struct sock, sk_pacing_status);
9041c95dd6SJörn-Thorben Hinz 		break;
910baf26b0SMartin KaFai Lau 	case bpf_ctx_range(struct inet_connection_sock, icsk_ca_priv):
920baf26b0SMartin KaFai Lau 		end = offsetofend(struct inet_connection_sock, icsk_ca_priv);
930baf26b0SMartin KaFai Lau 		break;
940baf26b0SMartin KaFai Lau 	case offsetof(struct inet_connection_sock, icsk_ack.pending):
950baf26b0SMartin KaFai Lau 		end = offsetofend(struct inet_connection_sock,
960baf26b0SMartin KaFai Lau 				  icsk_ack.pending);
970baf26b0SMartin KaFai Lau 		break;
980baf26b0SMartin KaFai Lau 	case offsetof(struct tcp_sock, snd_cwnd):
990baf26b0SMartin KaFai Lau 		end = offsetofend(struct tcp_sock, snd_cwnd);
1000baf26b0SMartin KaFai Lau 		break;
1010baf26b0SMartin KaFai Lau 	case offsetof(struct tcp_sock, snd_cwnd_cnt):
1020baf26b0SMartin KaFai Lau 		end = offsetofend(struct tcp_sock, snd_cwnd_cnt);
1030baf26b0SMartin KaFai Lau 		break;
1040baf26b0SMartin KaFai Lau 	case offsetof(struct tcp_sock, snd_ssthresh):
1050baf26b0SMartin KaFai Lau 		end = offsetofend(struct tcp_sock, snd_ssthresh);
1060baf26b0SMartin KaFai Lau 		break;
1070baf26b0SMartin KaFai Lau 	case offsetof(struct tcp_sock, ecn_flags):
1080baf26b0SMartin KaFai Lau 		end = offsetofend(struct tcp_sock, ecn_flags);
1090baf26b0SMartin KaFai Lau 		break;
110562dc56aSYixin Shen 	case offsetof(struct tcp_sock, app_limited):
111562dc56aSYixin Shen 		end = offsetofend(struct tcp_sock, app_limited);
112562dc56aSYixin Shen 		break;
1130baf26b0SMartin KaFai Lau 	default:
1140baf26b0SMartin KaFai Lau 		bpf_log(log, "no write support to tcp_sock at off %d\n", off);
1150baf26b0SMartin KaFai Lau 		return -EACCES;
1160baf26b0SMartin KaFai Lau 	}
1170baf26b0SMartin KaFai Lau 
1180baf26b0SMartin KaFai Lau 	if (off + size > end) {
1190baf26b0SMartin KaFai Lau 		bpf_log(log,
1200baf26b0SMartin KaFai Lau 			"write access at off %d with size %d beyond the member of tcp_sock ended at %zu\n",
1210baf26b0SMartin KaFai Lau 			off, size, end);
1220baf26b0SMartin KaFai Lau 		return -EACCES;
1230baf26b0SMartin KaFai Lau 	}
1240baf26b0SMartin KaFai Lau 
125896f07c0SDaniel Xu 	return 0;
1260baf26b0SMartin KaFai Lau }
1270baf26b0SMartin KaFai Lau 
BPF_CALL_2(bpf_tcp_send_ack,struct tcp_sock *,tp,u32,rcv_nxt)128206057feSMartin KaFai Lau BPF_CALL_2(bpf_tcp_send_ack, struct tcp_sock *, tp, u32, rcv_nxt)
129206057feSMartin KaFai Lau {
130206057feSMartin KaFai Lau 	/* bpf_tcp_ca prog cannot have NULL tp */
131206057feSMartin KaFai Lau 	__tcp_send_ack((struct sock *)tp, rcv_nxt);
132206057feSMartin KaFai Lau 	return 0;
133206057feSMartin KaFai Lau }
134206057feSMartin KaFai Lau 
135206057feSMartin KaFai Lau static const struct bpf_func_proto bpf_tcp_send_ack_proto = {
136206057feSMartin KaFai Lau 	.func		= bpf_tcp_send_ack,
137206057feSMartin KaFai Lau 	.gpl_only	= false,
138206057feSMartin KaFai Lau 	/* In case we want to report error later */
139206057feSMartin KaFai Lau 	.ret_type	= RET_INTEGER,
140206057feSMartin KaFai Lau 	.arg1_type	= ARG_PTR_TO_BTF_ID,
1419436ef6eSLorenz Bauer 	.arg1_btf_id	= &tcp_sock_id,
142206057feSMartin KaFai Lau 	.arg2_type	= ARG_ANYTHING,
143206057feSMartin KaFai Lau };
144206057feSMartin KaFai Lau 
prog_ops_moff(const struct bpf_prog * prog)145eb18b49eSMartin KaFai Lau static u32 prog_ops_moff(const struct bpf_prog *prog)
146eb18b49eSMartin KaFai Lau {
147eb18b49eSMartin KaFai Lau 	const struct btf_member *m;
148eb18b49eSMartin KaFai Lau 	const struct btf_type *t;
149eb18b49eSMartin KaFai Lau 	u32 midx;
150eb18b49eSMartin KaFai Lau 
151eb18b49eSMartin KaFai Lau 	midx = prog->expected_attach_type;
152eb18b49eSMartin KaFai Lau 	t = bpf_tcp_congestion_ops.type;
153eb18b49eSMartin KaFai Lau 	m = &btf_type_member(t)[midx];
154eb18b49eSMartin KaFai Lau 
1558293eb99SAlexei Starovoitov 	return __btf_member_bit_offset(t, m) / 8;
156eb18b49eSMartin KaFai Lau }
157eb18b49eSMartin KaFai Lau 
1580baf26b0SMartin KaFai Lau static const struct bpf_func_proto *
bpf_tcp_ca_get_func_proto(enum bpf_func_id func_id,const struct bpf_prog * prog)1590baf26b0SMartin KaFai Lau bpf_tcp_ca_get_func_proto(enum bpf_func_id func_id,
1600baf26b0SMartin KaFai Lau 			  const struct bpf_prog *prog)
1610baf26b0SMartin KaFai Lau {
162206057feSMartin KaFai Lau 	switch (func_id) {
163206057feSMartin KaFai Lau 	case BPF_FUNC_tcp_send_ack:
164206057feSMartin KaFai Lau 		return &bpf_tcp_send_ack_proto;
165ab14fd4eSMartin KaFai Lau 	case BPF_FUNC_sk_storage_get:
166592a3498SMartin KaFai Lau 		return &bpf_sk_storage_get_proto;
167ab14fd4eSMartin KaFai Lau 	case BPF_FUNC_sk_storage_delete:
168592a3498SMartin KaFai Lau 		return &bpf_sk_storage_delete_proto;
169eb18b49eSMartin KaFai Lau 	case BPF_FUNC_setsockopt:
170eb18b49eSMartin KaFai Lau 		/* Does not allow release() to call setsockopt.
171eb18b49eSMartin KaFai Lau 		 * release() is called when the current bpf-tcp-cc
172eb18b49eSMartin KaFai Lau 		 * is retiring.  It is not allowed to call
173eb18b49eSMartin KaFai Lau 		 * setsockopt() to make further changes which
174eb18b49eSMartin KaFai Lau 		 * may potentially allocate new resources.
175eb18b49eSMartin KaFai Lau 		 */
176eb18b49eSMartin KaFai Lau 		if (prog_ops_moff(prog) !=
177eb18b49eSMartin KaFai Lau 		    offsetof(struct tcp_congestion_ops, release))
178eb18b49eSMartin KaFai Lau 			return &bpf_sk_setsockopt_proto;
179eb18b49eSMartin KaFai Lau 		return NULL;
180eb18b49eSMartin KaFai Lau 	case BPF_FUNC_getsockopt:
181eb18b49eSMartin KaFai Lau 		/* Since get/setsockopt is usually expected to
182eb18b49eSMartin KaFai Lau 		 * be available together, disable getsockopt for
183eb18b49eSMartin KaFai Lau 		 * release also to avoid usage surprise.
184eb18b49eSMartin KaFai Lau 		 * The bpf-tcp-cc already has a more powerful way
185eb18b49eSMartin KaFai Lau 		 * to read tcp_sock from the PTR_TO_BTF_ID.
186eb18b49eSMartin KaFai Lau 		 */
187eb18b49eSMartin KaFai Lau 		if (prog_ops_moff(prog) !=
188eb18b49eSMartin KaFai Lau 		    offsetof(struct tcp_congestion_ops, release))
189eb18b49eSMartin KaFai Lau 			return &bpf_sk_getsockopt_proto;
190eb18b49eSMartin KaFai Lau 		return NULL;
1915e0bc308SDmitrii Banshchikov 	case BPF_FUNC_ktime_get_coarse_ns:
1925e0bc308SDmitrii Banshchikov 		return &bpf_ktime_get_coarse_ns_proto;
193206057feSMartin KaFai Lau 	default:
1940baf26b0SMartin KaFai Lau 		return bpf_base_func_proto(func_id);
1950baf26b0SMartin KaFai Lau 	}
196206057feSMartin KaFai Lau }
1970baf26b0SMartin KaFai Lau 
198a4703e31SKumar Kartikeya Dwivedi BTF_SET8_START(bpf_tcp_ca_check_kfunc_ids)
199a4703e31SKumar Kartikeya Dwivedi BTF_ID_FLAGS(func, tcp_reno_ssthresh)
200a4703e31SKumar Kartikeya Dwivedi BTF_ID_FLAGS(func, tcp_reno_cong_avoid)
201a4703e31SKumar Kartikeya Dwivedi BTF_ID_FLAGS(func, tcp_reno_undo_cwnd)
202a4703e31SKumar Kartikeya Dwivedi BTF_ID_FLAGS(func, tcp_slow_start)
203a4703e31SKumar Kartikeya Dwivedi BTF_ID_FLAGS(func, tcp_cong_avoid_ai)
204a4703e31SKumar Kartikeya Dwivedi BTF_SET8_END(bpf_tcp_ca_check_kfunc_ids)
205e78aea8bSMartin KaFai Lau 
206b202d844SKumar Kartikeya Dwivedi static const struct btf_kfunc_id_set bpf_tcp_ca_kfunc_set = {
207b202d844SKumar Kartikeya Dwivedi 	.owner = THIS_MODULE,
208a4703e31SKumar Kartikeya Dwivedi 	.set   = &bpf_tcp_ca_check_kfunc_ids,
209b202d844SKumar Kartikeya Dwivedi };
210e78aea8bSMartin KaFai Lau 
2110baf26b0SMartin KaFai Lau static const struct bpf_verifier_ops bpf_tcp_ca_verifier_ops = {
2120baf26b0SMartin KaFai Lau 	.get_func_proto		= bpf_tcp_ca_get_func_proto,
2130baf26b0SMartin KaFai Lau 	.is_valid_access	= bpf_tcp_ca_is_valid_access,
2140baf26b0SMartin KaFai Lau 	.btf_struct_access	= bpf_tcp_ca_btf_struct_access,
2150baf26b0SMartin KaFai Lau };
2160baf26b0SMartin KaFai Lau 
bpf_tcp_ca_init_member(const struct btf_type * t,const struct btf_member * member,void * kdata,const void * udata)2170baf26b0SMartin KaFai Lau static int bpf_tcp_ca_init_member(const struct btf_type *t,
2180baf26b0SMartin KaFai Lau 				  const struct btf_member *member,
2190baf26b0SMartin KaFai Lau 				  void *kdata, const void *udata)
2200baf26b0SMartin KaFai Lau {
2210baf26b0SMartin KaFai Lau 	const struct tcp_congestion_ops *utcp_ca;
2220baf26b0SMartin KaFai Lau 	struct tcp_congestion_ops *tcp_ca;
2230baf26b0SMartin KaFai Lau 	u32 moff;
2240baf26b0SMartin KaFai Lau 
2250baf26b0SMartin KaFai Lau 	utcp_ca = (const struct tcp_congestion_ops *)udata;
2260baf26b0SMartin KaFai Lau 	tcp_ca = (struct tcp_congestion_ops *)kdata;
2270baf26b0SMartin KaFai Lau 
2288293eb99SAlexei Starovoitov 	moff = __btf_member_bit_offset(t, member) / 8;
2290baf26b0SMartin KaFai Lau 	switch (moff) {
2300baf26b0SMartin KaFai Lau 	case offsetof(struct tcp_congestion_ops, flags):
2310baf26b0SMartin KaFai Lau 		if (utcp_ca->flags & ~TCP_CONG_MASK)
2320baf26b0SMartin KaFai Lau 			return -EINVAL;
2330baf26b0SMartin KaFai Lau 		tcp_ca->flags = utcp_ca->flags;
2340baf26b0SMartin KaFai Lau 		return 1;
2350baf26b0SMartin KaFai Lau 	case offsetof(struct tcp_congestion_ops, name):
2368e7ae251SMartin KaFai Lau 		if (bpf_obj_name_cpy(tcp_ca->name, utcp_ca->name,
2378e7ae251SMartin KaFai Lau 				     sizeof(tcp_ca->name)) <= 0)
2380baf26b0SMartin KaFai Lau 			return -EINVAL;
2390baf26b0SMartin KaFai Lau 		return 1;
2400baf26b0SMartin KaFai Lau 	}
2410baf26b0SMartin KaFai Lau 
2420baf26b0SMartin KaFai Lau 	return 0;
2430baf26b0SMartin KaFai Lau }
2440baf26b0SMartin KaFai Lau 
bpf_tcp_ca_check_member(const struct btf_type * t,const struct btf_member * member,const struct bpf_prog * prog)2450baf26b0SMartin KaFai Lau static int bpf_tcp_ca_check_member(const struct btf_type *t,
24651a52a29SDavid Vernet 				   const struct btf_member *member,
24751a52a29SDavid Vernet 				   const struct bpf_prog *prog)
2480baf26b0SMartin KaFai Lau {
2498293eb99SAlexei Starovoitov 	if (is_unsupported(__btf_member_bit_offset(t, member) / 8))
2500baf26b0SMartin KaFai Lau 		return -ENOTSUPP;
2510baf26b0SMartin KaFai Lau 	return 0;
2520baf26b0SMartin KaFai Lau }
2530baf26b0SMartin KaFai Lau 
bpf_tcp_ca_reg(void * kdata)2540baf26b0SMartin KaFai Lau static int bpf_tcp_ca_reg(void *kdata)
2550baf26b0SMartin KaFai Lau {
2560baf26b0SMartin KaFai Lau 	return tcp_register_congestion_control(kdata);
2570baf26b0SMartin KaFai Lau }
2580baf26b0SMartin KaFai Lau 
bpf_tcp_ca_unreg(void * kdata)2590baf26b0SMartin KaFai Lau static void bpf_tcp_ca_unreg(void *kdata)
2600baf26b0SMartin KaFai Lau {
2610baf26b0SMartin KaFai Lau 	tcp_unregister_congestion_control(kdata);
2620baf26b0SMartin KaFai Lau }
2630baf26b0SMartin KaFai Lau 
bpf_tcp_ca_update(void * kdata,void * old_kdata)264aef56f2eSKui-Feng Lee static int bpf_tcp_ca_update(void *kdata, void *old_kdata)
265aef56f2eSKui-Feng Lee {
266aef56f2eSKui-Feng Lee 	return tcp_update_congestion_control(kdata, old_kdata);
267aef56f2eSKui-Feng Lee }
268aef56f2eSKui-Feng Lee 
bpf_tcp_ca_validate(void * kdata)26968b04864SKui-Feng Lee static int bpf_tcp_ca_validate(void *kdata)
27068b04864SKui-Feng Lee {
27168b04864SKui-Feng Lee 	return tcp_validate_congestion_control(kdata);
27268b04864SKui-Feng Lee }
27368b04864SKui-Feng Lee 
2740baf26b0SMartin KaFai Lau struct bpf_struct_ops bpf_tcp_congestion_ops = {
2750baf26b0SMartin KaFai Lau 	.verifier_ops = &bpf_tcp_ca_verifier_ops,
2760baf26b0SMartin KaFai Lau 	.reg = bpf_tcp_ca_reg,
2770baf26b0SMartin KaFai Lau 	.unreg = bpf_tcp_ca_unreg,
278aef56f2eSKui-Feng Lee 	.update = bpf_tcp_ca_update,
2790baf26b0SMartin KaFai Lau 	.check_member = bpf_tcp_ca_check_member,
2800baf26b0SMartin KaFai Lau 	.init_member = bpf_tcp_ca_init_member,
2810baf26b0SMartin KaFai Lau 	.init = bpf_tcp_ca_init,
28268b04864SKui-Feng Lee 	.validate = bpf_tcp_ca_validate,
2830baf26b0SMartin KaFai Lau 	.name = "tcp_congestion_ops",
2840baf26b0SMartin KaFai Lau };
285b202d844SKumar Kartikeya Dwivedi 
bpf_tcp_ca_kfunc_init(void)286b202d844SKumar Kartikeya Dwivedi static int __init bpf_tcp_ca_kfunc_init(void)
287b202d844SKumar Kartikeya Dwivedi {
288b202d844SKumar Kartikeya Dwivedi 	return register_btf_kfunc_id_set(BPF_PROG_TYPE_STRUCT_OPS, &bpf_tcp_ca_kfunc_set);
289b202d844SKumar Kartikeya Dwivedi }
290b202d844SKumar Kartikeya Dwivedi late_initcall(bpf_tcp_ca_kfunc_init);
291