bpf_tcp_ca.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright (c) 2019 Facebook */
  3. #include <linux/types.h>
  4. #include <linux/bpf_verifier.h>
  5. #include <linux/bpf.h>
  6. #include <linux/btf.h>
  7. #include <linux/filter.h>
  8. #include <net/tcp.h>
  9. #include <net/bpf_sk_storage.h>
  10. static u32 optional_ops[] = {
  11. offsetof(struct tcp_congestion_ops, init),
  12. offsetof(struct tcp_congestion_ops, release),
  13. offsetof(struct tcp_congestion_ops, set_state),
  14. offsetof(struct tcp_congestion_ops, cwnd_event),
  15. offsetof(struct tcp_congestion_ops, in_ack_event),
  16. offsetof(struct tcp_congestion_ops, pkts_acked),
  17. offsetof(struct tcp_congestion_ops, min_tso_segs),
  18. offsetof(struct tcp_congestion_ops, sndbuf_expand),
  19. offsetof(struct tcp_congestion_ops, cong_control),
  20. };
  21. static u32 unsupported_ops[] = {
  22. offsetof(struct tcp_congestion_ops, get_info),
  23. };
  24. static const struct btf_type *tcp_sock_type;
  25. static u32 tcp_sock_id, sock_id;
  26. static int bpf_tcp_ca_init(struct btf *btf)
  27. {
  28. s32 type_id;
  29. type_id = btf_find_by_name_kind(btf, "sock", BTF_KIND_STRUCT);
  30. if (type_id < 0)
  31. return -EINVAL;
  32. sock_id = type_id;
  33. type_id = btf_find_by_name_kind(btf, "tcp_sock", BTF_KIND_STRUCT);
  34. if (type_id < 0)
  35. return -EINVAL;
  36. tcp_sock_id = type_id;
  37. tcp_sock_type = btf_type_by_id(btf, tcp_sock_id);
  38. return 0;
  39. }
  40. static bool is_optional(u32 member_offset)
  41. {
  42. unsigned int i;
  43. for (i = 0; i < ARRAY_SIZE(optional_ops); i++) {
  44. if (member_offset == optional_ops[i])
  45. return true;
  46. }
  47. return false;
  48. }
  49. static bool is_unsupported(u32 member_offset)
  50. {
  51. unsigned int i;
  52. for (i = 0; i < ARRAY_SIZE(unsupported_ops); i++) {
  53. if (member_offset == unsupported_ops[i])
  54. return true;
  55. }
  56. return false;
  57. }
  58. extern struct btf *btf_vmlinux;
  59. static bool bpf_tcp_ca_is_valid_access(int off, int size,
  60. enum bpf_access_type type,
  61. const struct bpf_prog *prog,
  62. struct bpf_insn_access_aux *info)
  63. {
  64. if (off < 0 || off >= sizeof(__u64) * MAX_BPF_FUNC_ARGS)
  65. return false;
  66. if (type != BPF_READ)
  67. return false;
  68. if (off % size != 0)
  69. return false;
  70. if (!btf_ctx_access(off, size, type, prog, info))
  71. return false;
  72. if (info->reg_type == PTR_TO_BTF_ID && info->btf_id == sock_id)
  73. /* promote it to tcp_sock */
  74. info->btf_id = tcp_sock_id;
  75. return true;
  76. }
  77. static int bpf_tcp_ca_btf_struct_access(struct bpf_verifier_log *log,
  78. const struct btf_type *t, int off,
  79. int size, enum bpf_access_type atype,
  80. u32 *next_btf_id)
  81. {
  82. size_t end;
  83. if (atype == BPF_READ)
  84. return btf_struct_access(log, t, off, size, atype, next_btf_id);
  85. if (t != tcp_sock_type) {
  86. bpf_log(log, "only read is supported\n");
  87. return -EACCES;
  88. }
  89. switch (off) {
  90. case bpf_ctx_range(struct inet_connection_sock, icsk_ca_priv):
  91. end = offsetofend(struct inet_connection_sock, icsk_ca_priv);
  92. break;
  93. case offsetof(struct inet_connection_sock, icsk_ack.pending):
  94. end = offsetofend(struct inet_connection_sock,
  95. icsk_ack.pending);
  96. break;
  97. case offsetof(struct tcp_sock, snd_cwnd):
  98. end = offsetofend(struct tcp_sock, snd_cwnd);
  99. break;
  100. case offsetof(struct tcp_sock, snd_cwnd_cnt):
  101. end = offsetofend(struct tcp_sock, snd_cwnd_cnt);
  102. break;
  103. case offsetof(struct tcp_sock, snd_ssthresh):
  104. end = offsetofend(struct tcp_sock, snd_ssthresh);
  105. break;
  106. case offsetof(struct tcp_sock, ecn_flags):
  107. end = offsetofend(struct tcp_sock, ecn_flags);
  108. break;
  109. default:
  110. bpf_log(log, "no write support to tcp_sock at off %d\n", off);
  111. return -EACCES;
  112. }
  113. if (off + size > end) {
  114. bpf_log(log,
  115. "write access at off %d with size %d beyond the member of tcp_sock ended at %zu\n",
  116. off, size, end);
  117. return -EACCES;
  118. }
  119. return NOT_INIT;
  120. }
  121. BPF_CALL_2(bpf_tcp_send_ack, struct tcp_sock *, tp, u32, rcv_nxt)
  122. {
  123. /* bpf_tcp_ca prog cannot have NULL tp */
  124. __tcp_send_ack((struct sock *)tp, rcv_nxt);
  125. return 0;
  126. }
  127. static const struct bpf_func_proto bpf_tcp_send_ack_proto = {
  128. .func = bpf_tcp_send_ack,
  129. .gpl_only = false,
  130. /* In case we want to report error later */
  131. .ret_type = RET_INTEGER,
  132. .arg1_type = ARG_PTR_TO_BTF_ID,
  133. .arg1_btf_id = &tcp_sock_id,
  134. .arg2_type = ARG_ANYTHING,
  135. };
  136. static const struct bpf_func_proto *
  137. bpf_tcp_ca_get_func_proto(enum bpf_func_id func_id,
  138. const struct bpf_prog *prog)
  139. {
  140. switch (func_id) {
  141. case BPF_FUNC_tcp_send_ack:
  142. return &bpf_tcp_send_ack_proto;
  143. case BPF_FUNC_sk_storage_get:
  144. return &bpf_sk_storage_get_proto;
  145. case BPF_FUNC_sk_storage_delete:
  146. return &bpf_sk_storage_delete_proto;
  147. default:
  148. return bpf_base_func_proto(func_id);
  149. }
  150. }
  151. static const struct bpf_verifier_ops bpf_tcp_ca_verifier_ops = {
  152. .get_func_proto = bpf_tcp_ca_get_func_proto,
  153. .is_valid_access = bpf_tcp_ca_is_valid_access,
  154. .btf_struct_access = bpf_tcp_ca_btf_struct_access,
  155. };
  156. static int bpf_tcp_ca_init_member(const struct btf_type *t,
  157. const struct btf_member *member,
  158. void *kdata, const void *udata)
  159. {
  160. const struct tcp_congestion_ops *utcp_ca;
  161. struct tcp_congestion_ops *tcp_ca;
  162. int prog_fd;
  163. u32 moff;
  164. utcp_ca = (const struct tcp_congestion_ops *)udata;
  165. tcp_ca = (struct tcp_congestion_ops *)kdata;
  166. moff = btf_member_bit_offset(t, member) / 8;
  167. switch (moff) {
  168. case offsetof(struct tcp_congestion_ops, flags):
  169. if (utcp_ca->flags & ~TCP_CONG_MASK)
  170. return -EINVAL;
  171. tcp_ca->flags = utcp_ca->flags;
  172. return 1;
  173. case offsetof(struct tcp_congestion_ops, name):
  174. if (bpf_obj_name_cpy(tcp_ca->name, utcp_ca->name,
  175. sizeof(tcp_ca->name)) <= 0)
  176. return -EINVAL;
  177. if (tcp_ca_find(utcp_ca->name))
  178. return -EEXIST;
  179. return 1;
  180. }
  181. if (!btf_type_resolve_func_ptr(btf_vmlinux, member->type, NULL))
  182. return 0;
  183. /* Ensure bpf_prog is provided for compulsory func ptr */
  184. prog_fd = (int)(*(unsigned long *)(udata + moff));
  185. if (!prog_fd && !is_optional(moff) && !is_unsupported(moff))
  186. return -EINVAL;
  187. return 0;
  188. }
  189. static int bpf_tcp_ca_check_member(const struct btf_type *t,
  190. const struct btf_member *member)
  191. {
  192. if (is_unsupported(btf_member_bit_offset(t, member) / 8))
  193. return -ENOTSUPP;
  194. return 0;
  195. }
  196. static int bpf_tcp_ca_reg(void *kdata)
  197. {
  198. return tcp_register_congestion_control(kdata);
  199. }
  200. static void bpf_tcp_ca_unreg(void *kdata)
  201. {
  202. tcp_unregister_congestion_control(kdata);
  203. }
  204. /* Avoid sparse warning. It is only used in bpf_struct_ops.c. */
  205. extern struct bpf_struct_ops bpf_tcp_congestion_ops;
  206. struct bpf_struct_ops bpf_tcp_congestion_ops = {
  207. .verifier_ops = &bpf_tcp_ca_verifier_ops,
  208. .reg = bpf_tcp_ca_reg,
  209. .unreg = bpf_tcp_ca_unreg,
  210. .check_member = bpf_tcp_ca_check_member,
  211. .init_member = bpf_tcp_ca_init_member,
  212. .init = bpf_tcp_ca_init,
  213. .name = "tcp_congestion_ops",
  214. };