xref: /openbmc/linux/net/ipv4/bpf_tcp_ca.c (revision 8e7ae2518f5265f0ef09d561748098fde5a87ccd)
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>
80baf26b0SMartin KaFai Lau #include <linux/filter.h>
90baf26b0SMartin KaFai Lau #include <net/tcp.h>
100baf26b0SMartin KaFai Lau 
110baf26b0SMartin KaFai Lau static u32 optional_ops[] = {
120baf26b0SMartin KaFai Lau 	offsetof(struct tcp_congestion_ops, init),
130baf26b0SMartin KaFai Lau 	offsetof(struct tcp_congestion_ops, release),
140baf26b0SMartin KaFai Lau 	offsetof(struct tcp_congestion_ops, set_state),
150baf26b0SMartin KaFai Lau 	offsetof(struct tcp_congestion_ops, cwnd_event),
160baf26b0SMartin KaFai Lau 	offsetof(struct tcp_congestion_ops, in_ack_event),
170baf26b0SMartin KaFai Lau 	offsetof(struct tcp_congestion_ops, pkts_acked),
180baf26b0SMartin KaFai Lau 	offsetof(struct tcp_congestion_ops, min_tso_segs),
190baf26b0SMartin KaFai Lau 	offsetof(struct tcp_congestion_ops, sndbuf_expand),
200baf26b0SMartin KaFai Lau 	offsetof(struct tcp_congestion_ops, cong_control),
210baf26b0SMartin KaFai Lau };
220baf26b0SMartin KaFai Lau 
230baf26b0SMartin KaFai Lau static u32 unsupported_ops[] = {
240baf26b0SMartin KaFai Lau 	offsetof(struct tcp_congestion_ops, get_info),
250baf26b0SMartin KaFai Lau };
260baf26b0SMartin KaFai Lau 
270baf26b0SMartin KaFai Lau static const struct btf_type *tcp_sock_type;
280baf26b0SMartin KaFai Lau static u32 tcp_sock_id, sock_id;
290baf26b0SMartin KaFai Lau 
300baf26b0SMartin KaFai Lau static int bpf_tcp_ca_init(struct btf *btf)
310baf26b0SMartin KaFai Lau {
320baf26b0SMartin KaFai Lau 	s32 type_id;
330baf26b0SMartin KaFai Lau 
340baf26b0SMartin KaFai Lau 	type_id = btf_find_by_name_kind(btf, "sock", BTF_KIND_STRUCT);
350baf26b0SMartin KaFai Lau 	if (type_id < 0)
360baf26b0SMartin KaFai Lau 		return -EINVAL;
370baf26b0SMartin KaFai Lau 	sock_id = type_id;
380baf26b0SMartin KaFai Lau 
390baf26b0SMartin KaFai Lau 	type_id = btf_find_by_name_kind(btf, "tcp_sock", BTF_KIND_STRUCT);
400baf26b0SMartin KaFai Lau 	if (type_id < 0)
410baf26b0SMartin KaFai Lau 		return -EINVAL;
420baf26b0SMartin KaFai Lau 	tcp_sock_id = type_id;
430baf26b0SMartin KaFai Lau 	tcp_sock_type = btf_type_by_id(btf, tcp_sock_id);
440baf26b0SMartin KaFai Lau 
450baf26b0SMartin KaFai Lau 	return 0;
460baf26b0SMartin KaFai Lau }
470baf26b0SMartin KaFai Lau 
480baf26b0SMartin KaFai Lau static bool is_optional(u32 member_offset)
490baf26b0SMartin KaFai Lau {
500baf26b0SMartin KaFai Lau 	unsigned int i;
510baf26b0SMartin KaFai Lau 
520baf26b0SMartin KaFai Lau 	for (i = 0; i < ARRAY_SIZE(optional_ops); i++) {
530baf26b0SMartin KaFai Lau 		if (member_offset == optional_ops[i])
540baf26b0SMartin KaFai Lau 			return true;
550baf26b0SMartin KaFai Lau 	}
560baf26b0SMartin KaFai Lau 
570baf26b0SMartin KaFai Lau 	return false;
580baf26b0SMartin KaFai Lau }
590baf26b0SMartin KaFai Lau 
600baf26b0SMartin KaFai Lau static bool is_unsupported(u32 member_offset)
610baf26b0SMartin KaFai Lau {
620baf26b0SMartin KaFai Lau 	unsigned int i;
630baf26b0SMartin KaFai Lau 
640baf26b0SMartin KaFai Lau 	for (i = 0; i < ARRAY_SIZE(unsupported_ops); i++) {
650baf26b0SMartin KaFai Lau 		if (member_offset == unsupported_ops[i])
660baf26b0SMartin KaFai Lau 			return true;
670baf26b0SMartin KaFai Lau 	}
680baf26b0SMartin KaFai Lau 
690baf26b0SMartin KaFai Lau 	return false;
700baf26b0SMartin KaFai Lau }
710baf26b0SMartin KaFai Lau 
720baf26b0SMartin KaFai Lau extern struct btf *btf_vmlinux;
730baf26b0SMartin KaFai Lau 
740baf26b0SMartin KaFai Lau static bool bpf_tcp_ca_is_valid_access(int off, int size,
750baf26b0SMartin KaFai Lau 				       enum bpf_access_type type,
760baf26b0SMartin KaFai Lau 				       const struct bpf_prog *prog,
770baf26b0SMartin KaFai Lau 				       struct bpf_insn_access_aux *info)
780baf26b0SMartin KaFai Lau {
790baf26b0SMartin KaFai Lau 	if (off < 0 || off >= sizeof(__u64) * MAX_BPF_FUNC_ARGS)
800baf26b0SMartin KaFai Lau 		return false;
810baf26b0SMartin KaFai Lau 	if (type != BPF_READ)
820baf26b0SMartin KaFai Lau 		return false;
830baf26b0SMartin KaFai Lau 	if (off % size != 0)
840baf26b0SMartin KaFai Lau 		return false;
850baf26b0SMartin KaFai Lau 
860baf26b0SMartin KaFai Lau 	if (!btf_ctx_access(off, size, type, prog, info))
870baf26b0SMartin KaFai Lau 		return false;
880baf26b0SMartin KaFai Lau 
890baf26b0SMartin KaFai Lau 	if (info->reg_type == PTR_TO_BTF_ID && info->btf_id == sock_id)
900baf26b0SMartin KaFai Lau 		/* promote it to tcp_sock */
910baf26b0SMartin KaFai Lau 		info->btf_id = tcp_sock_id;
920baf26b0SMartin KaFai Lau 
930baf26b0SMartin KaFai Lau 	return true;
940baf26b0SMartin KaFai Lau }
950baf26b0SMartin KaFai Lau 
960baf26b0SMartin KaFai Lau static int bpf_tcp_ca_btf_struct_access(struct bpf_verifier_log *log,
970baf26b0SMartin KaFai Lau 					const struct btf_type *t, int off,
980baf26b0SMartin KaFai Lau 					int size, enum bpf_access_type atype,
990baf26b0SMartin KaFai Lau 					u32 *next_btf_id)
1000baf26b0SMartin KaFai Lau {
1010baf26b0SMartin KaFai Lau 	size_t end;
1020baf26b0SMartin KaFai Lau 
1030baf26b0SMartin KaFai Lau 	if (atype == BPF_READ)
1040baf26b0SMartin KaFai Lau 		return btf_struct_access(log, t, off, size, atype, next_btf_id);
1050baf26b0SMartin KaFai Lau 
1060baf26b0SMartin KaFai Lau 	if (t != tcp_sock_type) {
1070baf26b0SMartin KaFai Lau 		bpf_log(log, "only read is supported\n");
1080baf26b0SMartin KaFai Lau 		return -EACCES;
1090baf26b0SMartin KaFai Lau 	}
1100baf26b0SMartin KaFai Lau 
1110baf26b0SMartin KaFai Lau 	switch (off) {
1120baf26b0SMartin KaFai Lau 	case bpf_ctx_range(struct inet_connection_sock, icsk_ca_priv):
1130baf26b0SMartin KaFai Lau 		end = offsetofend(struct inet_connection_sock, icsk_ca_priv);
1140baf26b0SMartin KaFai Lau 		break;
1150baf26b0SMartin KaFai Lau 	case offsetof(struct inet_connection_sock, icsk_ack.pending):
1160baf26b0SMartin KaFai Lau 		end = offsetofend(struct inet_connection_sock,
1170baf26b0SMartin KaFai Lau 				  icsk_ack.pending);
1180baf26b0SMartin KaFai Lau 		break;
1190baf26b0SMartin KaFai Lau 	case offsetof(struct tcp_sock, snd_cwnd):
1200baf26b0SMartin KaFai Lau 		end = offsetofend(struct tcp_sock, snd_cwnd);
1210baf26b0SMartin KaFai Lau 		break;
1220baf26b0SMartin KaFai Lau 	case offsetof(struct tcp_sock, snd_cwnd_cnt):
1230baf26b0SMartin KaFai Lau 		end = offsetofend(struct tcp_sock, snd_cwnd_cnt);
1240baf26b0SMartin KaFai Lau 		break;
1250baf26b0SMartin KaFai Lau 	case offsetof(struct tcp_sock, snd_ssthresh):
1260baf26b0SMartin KaFai Lau 		end = offsetofend(struct tcp_sock, snd_ssthresh);
1270baf26b0SMartin KaFai Lau 		break;
1280baf26b0SMartin KaFai Lau 	case offsetof(struct tcp_sock, ecn_flags):
1290baf26b0SMartin KaFai Lau 		end = offsetofend(struct tcp_sock, ecn_flags);
1300baf26b0SMartin KaFai Lau 		break;
1310baf26b0SMartin KaFai Lau 	default:
1320baf26b0SMartin KaFai Lau 		bpf_log(log, "no write support to tcp_sock at off %d\n", off);
1330baf26b0SMartin KaFai Lau 		return -EACCES;
1340baf26b0SMartin KaFai Lau 	}
1350baf26b0SMartin KaFai Lau 
1360baf26b0SMartin KaFai Lau 	if (off + size > end) {
1370baf26b0SMartin KaFai Lau 		bpf_log(log,
1380baf26b0SMartin KaFai Lau 			"write access at off %d with size %d beyond the member of tcp_sock ended at %zu\n",
1390baf26b0SMartin KaFai Lau 			off, size, end);
1400baf26b0SMartin KaFai Lau 		return -EACCES;
1410baf26b0SMartin KaFai Lau 	}
1420baf26b0SMartin KaFai Lau 
1430baf26b0SMartin KaFai Lau 	return NOT_INIT;
1440baf26b0SMartin KaFai Lau }
1450baf26b0SMartin KaFai Lau 
146206057feSMartin KaFai Lau BPF_CALL_2(bpf_tcp_send_ack, struct tcp_sock *, tp, u32, rcv_nxt)
147206057feSMartin KaFai Lau {
148206057feSMartin KaFai Lau 	/* bpf_tcp_ca prog cannot have NULL tp */
149206057feSMartin KaFai Lau 	__tcp_send_ack((struct sock *)tp, rcv_nxt);
150206057feSMartin KaFai Lau 	return 0;
151206057feSMartin KaFai Lau }
152206057feSMartin KaFai Lau 
153206057feSMartin KaFai Lau static const struct bpf_func_proto bpf_tcp_send_ack_proto = {
154206057feSMartin KaFai Lau 	.func		= bpf_tcp_send_ack,
155206057feSMartin KaFai Lau 	.gpl_only	= false,
156206057feSMartin KaFai Lau 	/* In case we want to report error later */
157206057feSMartin KaFai Lau 	.ret_type	= RET_INTEGER,
158206057feSMartin KaFai Lau 	.arg1_type	= ARG_PTR_TO_BTF_ID,
159206057feSMartin KaFai Lau 	.arg2_type	= ARG_ANYTHING,
160206057feSMartin KaFai Lau 	.btf_id		= &tcp_sock_id,
161206057feSMartin KaFai Lau };
162206057feSMartin KaFai Lau 
1630baf26b0SMartin KaFai Lau static const struct bpf_func_proto *
1640baf26b0SMartin KaFai Lau bpf_tcp_ca_get_func_proto(enum bpf_func_id func_id,
1650baf26b0SMartin KaFai Lau 			  const struct bpf_prog *prog)
1660baf26b0SMartin KaFai Lau {
167206057feSMartin KaFai Lau 	switch (func_id) {
168206057feSMartin KaFai Lau 	case BPF_FUNC_tcp_send_ack:
169206057feSMartin KaFai Lau 		return &bpf_tcp_send_ack_proto;
170206057feSMartin KaFai Lau 	default:
1710baf26b0SMartin KaFai Lau 		return bpf_base_func_proto(func_id);
1720baf26b0SMartin KaFai Lau 	}
173206057feSMartin KaFai Lau }
1740baf26b0SMartin KaFai Lau 
1750baf26b0SMartin KaFai Lau static const struct bpf_verifier_ops bpf_tcp_ca_verifier_ops = {
1760baf26b0SMartin KaFai Lau 	.get_func_proto		= bpf_tcp_ca_get_func_proto,
1770baf26b0SMartin KaFai Lau 	.is_valid_access	= bpf_tcp_ca_is_valid_access,
1780baf26b0SMartin KaFai Lau 	.btf_struct_access	= bpf_tcp_ca_btf_struct_access,
1790baf26b0SMartin KaFai Lau };
1800baf26b0SMartin KaFai Lau 
1810baf26b0SMartin KaFai Lau static int bpf_tcp_ca_init_member(const struct btf_type *t,
1820baf26b0SMartin KaFai Lau 				  const struct btf_member *member,
1830baf26b0SMartin KaFai Lau 				  void *kdata, const void *udata)
1840baf26b0SMartin KaFai Lau {
1850baf26b0SMartin KaFai Lau 	const struct tcp_congestion_ops *utcp_ca;
1860baf26b0SMartin KaFai Lau 	struct tcp_congestion_ops *tcp_ca;
1870baf26b0SMartin KaFai Lau 	int prog_fd;
1880baf26b0SMartin KaFai Lau 	u32 moff;
1890baf26b0SMartin KaFai Lau 
1900baf26b0SMartin KaFai Lau 	utcp_ca = (const struct tcp_congestion_ops *)udata;
1910baf26b0SMartin KaFai Lau 	tcp_ca = (struct tcp_congestion_ops *)kdata;
1920baf26b0SMartin KaFai Lau 
1930baf26b0SMartin KaFai Lau 	moff = btf_member_bit_offset(t, member) / 8;
1940baf26b0SMartin KaFai Lau 	switch (moff) {
1950baf26b0SMartin KaFai Lau 	case offsetof(struct tcp_congestion_ops, flags):
1960baf26b0SMartin KaFai Lau 		if (utcp_ca->flags & ~TCP_CONG_MASK)
1970baf26b0SMartin KaFai Lau 			return -EINVAL;
1980baf26b0SMartin KaFai Lau 		tcp_ca->flags = utcp_ca->flags;
1990baf26b0SMartin KaFai Lau 		return 1;
2000baf26b0SMartin KaFai Lau 	case offsetof(struct tcp_congestion_ops, name):
201*8e7ae251SMartin KaFai Lau 		if (bpf_obj_name_cpy(tcp_ca->name, utcp_ca->name,
202*8e7ae251SMartin KaFai Lau 				     sizeof(tcp_ca->name)) <= 0)
2030baf26b0SMartin KaFai Lau 			return -EINVAL;
2040baf26b0SMartin KaFai Lau 		if (tcp_ca_find(utcp_ca->name))
2050baf26b0SMartin KaFai Lau 			return -EEXIST;
2060baf26b0SMartin KaFai Lau 		return 1;
2070baf26b0SMartin KaFai Lau 	}
2080baf26b0SMartin KaFai Lau 
2090baf26b0SMartin KaFai Lau 	if (!btf_type_resolve_func_ptr(btf_vmlinux, member->type, NULL))
2100baf26b0SMartin KaFai Lau 		return 0;
2110baf26b0SMartin KaFai Lau 
2120baf26b0SMartin KaFai Lau 	/* Ensure bpf_prog is provided for compulsory func ptr */
2130baf26b0SMartin KaFai Lau 	prog_fd = (int)(*(unsigned long *)(udata + moff));
2140baf26b0SMartin KaFai Lau 	if (!prog_fd && !is_optional(moff) && !is_unsupported(moff))
2150baf26b0SMartin KaFai Lau 		return -EINVAL;
2160baf26b0SMartin KaFai Lau 
2170baf26b0SMartin KaFai Lau 	return 0;
2180baf26b0SMartin KaFai Lau }
2190baf26b0SMartin KaFai Lau 
2200baf26b0SMartin KaFai Lau static int bpf_tcp_ca_check_member(const struct btf_type *t,
2210baf26b0SMartin KaFai Lau 				   const struct btf_member *member)
2220baf26b0SMartin KaFai Lau {
2230baf26b0SMartin KaFai Lau 	if (is_unsupported(btf_member_bit_offset(t, member) / 8))
2240baf26b0SMartin KaFai Lau 		return -ENOTSUPP;
2250baf26b0SMartin KaFai Lau 	return 0;
2260baf26b0SMartin KaFai Lau }
2270baf26b0SMartin KaFai Lau 
2280baf26b0SMartin KaFai Lau static int bpf_tcp_ca_reg(void *kdata)
2290baf26b0SMartin KaFai Lau {
2300baf26b0SMartin KaFai Lau 	return tcp_register_congestion_control(kdata);
2310baf26b0SMartin KaFai Lau }
2320baf26b0SMartin KaFai Lau 
2330baf26b0SMartin KaFai Lau static void bpf_tcp_ca_unreg(void *kdata)
2340baf26b0SMartin KaFai Lau {
2350baf26b0SMartin KaFai Lau 	tcp_unregister_congestion_control(kdata);
2360baf26b0SMartin KaFai Lau }
2370baf26b0SMartin KaFai Lau 
2380baf26b0SMartin KaFai Lau /* Avoid sparse warning.  It is only used in bpf_struct_ops.c. */
2390baf26b0SMartin KaFai Lau extern struct bpf_struct_ops bpf_tcp_congestion_ops;
2400baf26b0SMartin KaFai Lau 
2410baf26b0SMartin KaFai Lau struct bpf_struct_ops bpf_tcp_congestion_ops = {
2420baf26b0SMartin KaFai Lau 	.verifier_ops = &bpf_tcp_ca_verifier_ops,
2430baf26b0SMartin KaFai Lau 	.reg = bpf_tcp_ca_reg,
2440baf26b0SMartin KaFai Lau 	.unreg = bpf_tcp_ca_unreg,
2450baf26b0SMartin KaFai Lau 	.check_member = bpf_tcp_ca_check_member,
2460baf26b0SMartin KaFai Lau 	.init_member = bpf_tcp_ca_init_member,
2470baf26b0SMartin KaFai Lau 	.init = bpf_tcp_ca_init,
2480baf26b0SMartin KaFai Lau 	.name = "tcp_congestion_ops",
2490baf26b0SMartin KaFai Lau };
250