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, 75*b7e852a9SAlexei Starovoitov int off, int size) 760baf26b0SMartin KaFai Lau { 776728aea7SKumar Kartikeya Dwivedi const struct btf_type *t; 780baf26b0SMartin KaFai Lau size_t end; 790baf26b0SMartin KaFai Lau 806728aea7SKumar Kartikeya Dwivedi t = btf_type_by_id(reg->btf, reg->btf_id); 810baf26b0SMartin KaFai Lau if (t != tcp_sock_type) { 820baf26b0SMartin KaFai Lau bpf_log(log, "only read is supported\n"); 830baf26b0SMartin KaFai Lau return -EACCES; 840baf26b0SMartin KaFai Lau } 850baf26b0SMartin KaFai Lau 860baf26b0SMartin KaFai Lau switch (off) { 8741c95dd6SJörn-Thorben Hinz case offsetof(struct sock, sk_pacing_rate): 8841c95dd6SJörn-Thorben Hinz end = offsetofend(struct sock, sk_pacing_rate); 8941c95dd6SJörn-Thorben Hinz break; 9041c95dd6SJörn-Thorben Hinz case offsetof(struct sock, sk_pacing_status): 9141c95dd6SJörn-Thorben Hinz end = offsetofend(struct sock, sk_pacing_status); 9241c95dd6SJörn-Thorben Hinz break; 930baf26b0SMartin KaFai Lau case bpf_ctx_range(struct inet_connection_sock, icsk_ca_priv): 940baf26b0SMartin KaFai Lau end = offsetofend(struct inet_connection_sock, icsk_ca_priv); 950baf26b0SMartin KaFai Lau break; 960baf26b0SMartin KaFai Lau case offsetof(struct inet_connection_sock, icsk_ack.pending): 970baf26b0SMartin KaFai Lau end = offsetofend(struct inet_connection_sock, 980baf26b0SMartin KaFai Lau icsk_ack.pending); 990baf26b0SMartin KaFai Lau break; 1000baf26b0SMartin KaFai Lau case offsetof(struct tcp_sock, snd_cwnd): 1010baf26b0SMartin KaFai Lau end = offsetofend(struct tcp_sock, snd_cwnd); 1020baf26b0SMartin KaFai Lau break; 1030baf26b0SMartin KaFai Lau case offsetof(struct tcp_sock, snd_cwnd_cnt): 1040baf26b0SMartin KaFai Lau end = offsetofend(struct tcp_sock, snd_cwnd_cnt); 1050baf26b0SMartin KaFai Lau break; 1060baf26b0SMartin KaFai Lau case offsetof(struct tcp_sock, snd_ssthresh): 1070baf26b0SMartin KaFai Lau end = offsetofend(struct tcp_sock, snd_ssthresh); 1080baf26b0SMartin KaFai Lau break; 1090baf26b0SMartin KaFai Lau case offsetof(struct tcp_sock, ecn_flags): 1100baf26b0SMartin KaFai Lau end = offsetofend(struct tcp_sock, ecn_flags); 1110baf26b0SMartin KaFai Lau break; 112562dc56aSYixin Shen case offsetof(struct tcp_sock, app_limited): 113562dc56aSYixin Shen end = offsetofend(struct tcp_sock, app_limited); 114562dc56aSYixin Shen break; 1150baf26b0SMartin KaFai Lau default: 1160baf26b0SMartin KaFai Lau bpf_log(log, "no write support to tcp_sock at off %d\n", off); 1170baf26b0SMartin KaFai Lau return -EACCES; 1180baf26b0SMartin KaFai Lau } 1190baf26b0SMartin KaFai Lau 1200baf26b0SMartin KaFai Lau if (off + size > end) { 1210baf26b0SMartin KaFai Lau bpf_log(log, 1220baf26b0SMartin KaFai Lau "write access at off %d with size %d beyond the member of tcp_sock ended at %zu\n", 1230baf26b0SMartin KaFai Lau off, size, end); 1240baf26b0SMartin KaFai Lau return -EACCES; 1250baf26b0SMartin KaFai Lau } 1260baf26b0SMartin KaFai Lau 127896f07c0SDaniel Xu return 0; 1280baf26b0SMartin KaFai Lau } 1290baf26b0SMartin KaFai Lau 130206057feSMartin KaFai Lau BPF_CALL_2(bpf_tcp_send_ack, struct tcp_sock *, tp, u32, rcv_nxt) 131206057feSMartin KaFai Lau { 132206057feSMartin KaFai Lau /* bpf_tcp_ca prog cannot have NULL tp */ 133206057feSMartin KaFai Lau __tcp_send_ack((struct sock *)tp, rcv_nxt); 134206057feSMartin KaFai Lau return 0; 135206057feSMartin KaFai Lau } 136206057feSMartin KaFai Lau 137206057feSMartin KaFai Lau static const struct bpf_func_proto bpf_tcp_send_ack_proto = { 138206057feSMartin KaFai Lau .func = bpf_tcp_send_ack, 139206057feSMartin KaFai Lau .gpl_only = false, 140206057feSMartin KaFai Lau /* In case we want to report error later */ 141206057feSMartin KaFai Lau .ret_type = RET_INTEGER, 142206057feSMartin KaFai Lau .arg1_type = ARG_PTR_TO_BTF_ID, 1439436ef6eSLorenz Bauer .arg1_btf_id = &tcp_sock_id, 144206057feSMartin KaFai Lau .arg2_type = ARG_ANYTHING, 145206057feSMartin KaFai Lau }; 146206057feSMartin KaFai Lau 147eb18b49eSMartin KaFai Lau static u32 prog_ops_moff(const struct bpf_prog *prog) 148eb18b49eSMartin KaFai Lau { 149eb18b49eSMartin KaFai Lau const struct btf_member *m; 150eb18b49eSMartin KaFai Lau const struct btf_type *t; 151eb18b49eSMartin KaFai Lau u32 midx; 152eb18b49eSMartin KaFai Lau 153eb18b49eSMartin KaFai Lau midx = prog->expected_attach_type; 154eb18b49eSMartin KaFai Lau t = bpf_tcp_congestion_ops.type; 155eb18b49eSMartin KaFai Lau m = &btf_type_member(t)[midx]; 156eb18b49eSMartin KaFai Lau 1578293eb99SAlexei Starovoitov return __btf_member_bit_offset(t, m) / 8; 158eb18b49eSMartin KaFai Lau } 159eb18b49eSMartin KaFai Lau 1600baf26b0SMartin KaFai Lau static const struct bpf_func_proto * 1610baf26b0SMartin KaFai Lau bpf_tcp_ca_get_func_proto(enum bpf_func_id func_id, 1620baf26b0SMartin KaFai Lau const struct bpf_prog *prog) 1630baf26b0SMartin KaFai Lau { 164206057feSMartin KaFai Lau switch (func_id) { 165206057feSMartin KaFai Lau case BPF_FUNC_tcp_send_ack: 166206057feSMartin KaFai Lau return &bpf_tcp_send_ack_proto; 167ab14fd4eSMartin KaFai Lau case BPF_FUNC_sk_storage_get: 168592a3498SMartin KaFai Lau return &bpf_sk_storage_get_proto; 169ab14fd4eSMartin KaFai Lau case BPF_FUNC_sk_storage_delete: 170592a3498SMartin KaFai Lau return &bpf_sk_storage_delete_proto; 171eb18b49eSMartin KaFai Lau case BPF_FUNC_setsockopt: 172eb18b49eSMartin KaFai Lau /* Does not allow release() to call setsockopt. 173eb18b49eSMartin KaFai Lau * release() is called when the current bpf-tcp-cc 174eb18b49eSMartin KaFai Lau * is retiring. It is not allowed to call 175eb18b49eSMartin KaFai Lau * setsockopt() to make further changes which 176eb18b49eSMartin KaFai Lau * may potentially allocate new resources. 177eb18b49eSMartin KaFai Lau */ 178eb18b49eSMartin KaFai Lau if (prog_ops_moff(prog) != 179eb18b49eSMartin KaFai Lau offsetof(struct tcp_congestion_ops, release)) 180eb18b49eSMartin KaFai Lau return &bpf_sk_setsockopt_proto; 181eb18b49eSMartin KaFai Lau return NULL; 182eb18b49eSMartin KaFai Lau case BPF_FUNC_getsockopt: 183eb18b49eSMartin KaFai Lau /* Since get/setsockopt is usually expected to 184eb18b49eSMartin KaFai Lau * be available together, disable getsockopt for 185eb18b49eSMartin KaFai Lau * release also to avoid usage surprise. 186eb18b49eSMartin KaFai Lau * The bpf-tcp-cc already has a more powerful way 187eb18b49eSMartin KaFai Lau * to read tcp_sock from the PTR_TO_BTF_ID. 188eb18b49eSMartin KaFai Lau */ 189eb18b49eSMartin KaFai Lau if (prog_ops_moff(prog) != 190eb18b49eSMartin KaFai Lau offsetof(struct tcp_congestion_ops, release)) 191eb18b49eSMartin KaFai Lau return &bpf_sk_getsockopt_proto; 192eb18b49eSMartin KaFai Lau return NULL; 1935e0bc308SDmitrii Banshchikov case BPF_FUNC_ktime_get_coarse_ns: 1945e0bc308SDmitrii Banshchikov return &bpf_ktime_get_coarse_ns_proto; 195206057feSMartin KaFai Lau default: 1960baf26b0SMartin KaFai Lau return bpf_base_func_proto(func_id); 1970baf26b0SMartin KaFai Lau } 198206057feSMartin KaFai Lau } 1990baf26b0SMartin KaFai Lau 200a4703e31SKumar Kartikeya Dwivedi BTF_SET8_START(bpf_tcp_ca_check_kfunc_ids) 201a4703e31SKumar Kartikeya Dwivedi BTF_ID_FLAGS(func, tcp_reno_ssthresh) 202a4703e31SKumar Kartikeya Dwivedi BTF_ID_FLAGS(func, tcp_reno_cong_avoid) 203a4703e31SKumar Kartikeya Dwivedi BTF_ID_FLAGS(func, tcp_reno_undo_cwnd) 204a4703e31SKumar Kartikeya Dwivedi BTF_ID_FLAGS(func, tcp_slow_start) 205a4703e31SKumar Kartikeya Dwivedi BTF_ID_FLAGS(func, tcp_cong_avoid_ai) 206a4703e31SKumar Kartikeya Dwivedi BTF_SET8_END(bpf_tcp_ca_check_kfunc_ids) 207e78aea8bSMartin KaFai Lau 208b202d844SKumar Kartikeya Dwivedi static const struct btf_kfunc_id_set bpf_tcp_ca_kfunc_set = { 209b202d844SKumar Kartikeya Dwivedi .owner = THIS_MODULE, 210a4703e31SKumar Kartikeya Dwivedi .set = &bpf_tcp_ca_check_kfunc_ids, 211b202d844SKumar Kartikeya Dwivedi }; 212e78aea8bSMartin KaFai Lau 2130baf26b0SMartin KaFai Lau static const struct bpf_verifier_ops bpf_tcp_ca_verifier_ops = { 2140baf26b0SMartin KaFai Lau .get_func_proto = bpf_tcp_ca_get_func_proto, 2150baf26b0SMartin KaFai Lau .is_valid_access = bpf_tcp_ca_is_valid_access, 2160baf26b0SMartin KaFai Lau .btf_struct_access = bpf_tcp_ca_btf_struct_access, 2170baf26b0SMartin KaFai Lau }; 2180baf26b0SMartin KaFai Lau 2190baf26b0SMartin KaFai Lau static int bpf_tcp_ca_init_member(const struct btf_type *t, 2200baf26b0SMartin KaFai Lau const struct btf_member *member, 2210baf26b0SMartin KaFai Lau void *kdata, const void *udata) 2220baf26b0SMartin KaFai Lau { 2230baf26b0SMartin KaFai Lau const struct tcp_congestion_ops *utcp_ca; 2240baf26b0SMartin KaFai Lau struct tcp_congestion_ops *tcp_ca; 2250baf26b0SMartin KaFai Lau u32 moff; 2260baf26b0SMartin KaFai Lau 2270baf26b0SMartin KaFai Lau utcp_ca = (const struct tcp_congestion_ops *)udata; 2280baf26b0SMartin KaFai Lau tcp_ca = (struct tcp_congestion_ops *)kdata; 2290baf26b0SMartin KaFai Lau 2308293eb99SAlexei Starovoitov moff = __btf_member_bit_offset(t, member) / 8; 2310baf26b0SMartin KaFai Lau switch (moff) { 2320baf26b0SMartin KaFai Lau case offsetof(struct tcp_congestion_ops, flags): 2330baf26b0SMartin KaFai Lau if (utcp_ca->flags & ~TCP_CONG_MASK) 2340baf26b0SMartin KaFai Lau return -EINVAL; 2350baf26b0SMartin KaFai Lau tcp_ca->flags = utcp_ca->flags; 2360baf26b0SMartin KaFai Lau return 1; 2370baf26b0SMartin KaFai Lau case offsetof(struct tcp_congestion_ops, name): 2388e7ae251SMartin KaFai Lau if (bpf_obj_name_cpy(tcp_ca->name, utcp_ca->name, 2398e7ae251SMartin KaFai Lau sizeof(tcp_ca->name)) <= 0) 2400baf26b0SMartin KaFai Lau return -EINVAL; 2410baf26b0SMartin KaFai Lau return 1; 2420baf26b0SMartin KaFai Lau } 2430baf26b0SMartin KaFai Lau 2440baf26b0SMartin KaFai Lau return 0; 2450baf26b0SMartin KaFai Lau } 2460baf26b0SMartin KaFai Lau 2470baf26b0SMartin KaFai Lau static int bpf_tcp_ca_check_member(const struct btf_type *t, 24851a52a29SDavid Vernet const struct btf_member *member, 24951a52a29SDavid Vernet const struct bpf_prog *prog) 2500baf26b0SMartin KaFai Lau { 2518293eb99SAlexei Starovoitov if (is_unsupported(__btf_member_bit_offset(t, member) / 8)) 2520baf26b0SMartin KaFai Lau return -ENOTSUPP; 2530baf26b0SMartin KaFai Lau return 0; 2540baf26b0SMartin KaFai Lau } 2550baf26b0SMartin KaFai Lau 2560baf26b0SMartin KaFai Lau static int bpf_tcp_ca_reg(void *kdata) 2570baf26b0SMartin KaFai Lau { 2580baf26b0SMartin KaFai Lau return tcp_register_congestion_control(kdata); 2590baf26b0SMartin KaFai Lau } 2600baf26b0SMartin KaFai Lau 2610baf26b0SMartin KaFai Lau static void bpf_tcp_ca_unreg(void *kdata) 2620baf26b0SMartin KaFai Lau { 2630baf26b0SMartin KaFai Lau tcp_unregister_congestion_control(kdata); 2640baf26b0SMartin KaFai Lau } 2650baf26b0SMartin KaFai Lau 266aef56f2eSKui-Feng Lee static int bpf_tcp_ca_update(void *kdata, void *old_kdata) 267aef56f2eSKui-Feng Lee { 268aef56f2eSKui-Feng Lee return tcp_update_congestion_control(kdata, old_kdata); 269aef56f2eSKui-Feng Lee } 270aef56f2eSKui-Feng Lee 27168b04864SKui-Feng Lee static int bpf_tcp_ca_validate(void *kdata) 27268b04864SKui-Feng Lee { 27368b04864SKui-Feng Lee return tcp_validate_congestion_control(kdata); 27468b04864SKui-Feng Lee } 27568b04864SKui-Feng Lee 2760baf26b0SMartin KaFai Lau struct bpf_struct_ops bpf_tcp_congestion_ops = { 2770baf26b0SMartin KaFai Lau .verifier_ops = &bpf_tcp_ca_verifier_ops, 2780baf26b0SMartin KaFai Lau .reg = bpf_tcp_ca_reg, 2790baf26b0SMartin KaFai Lau .unreg = bpf_tcp_ca_unreg, 280aef56f2eSKui-Feng Lee .update = bpf_tcp_ca_update, 2810baf26b0SMartin KaFai Lau .check_member = bpf_tcp_ca_check_member, 2820baf26b0SMartin KaFai Lau .init_member = bpf_tcp_ca_init_member, 2830baf26b0SMartin KaFai Lau .init = bpf_tcp_ca_init, 28468b04864SKui-Feng Lee .validate = bpf_tcp_ca_validate, 2850baf26b0SMartin KaFai Lau .name = "tcp_congestion_ops", 2860baf26b0SMartin KaFai Lau }; 287b202d844SKumar Kartikeya Dwivedi 288b202d844SKumar Kartikeya Dwivedi static int __init bpf_tcp_ca_kfunc_init(void) 289b202d844SKumar Kartikeya Dwivedi { 290b202d844SKumar Kartikeya Dwivedi return register_btf_kfunc_id_set(BPF_PROG_TYPE_STRUCT_OPS, &bpf_tcp_ca_kfunc_set); 291b202d844SKumar Kartikeya Dwivedi } 292b202d844SKumar Kartikeya Dwivedi late_initcall(bpf_tcp_ca_kfunc_init); 293