tcp_bpf.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright (c) 2017 - 2018 Covalent IO, Inc. http://covalent.io */
  3. #include <linux/skmsg.h>
  4. #include <linux/filter.h>
  5. #include <linux/bpf.h>
  6. #include <linux/init.h>
  7. #include <linux/wait.h>
  8. #include <linux/util_macros.h>
  9. #include <net/inet_common.h>
  10. #include <net/tls.h>
  11. void tcp_eat_skb(struct sock *sk, struct sk_buff *skb)
  12. {
  13. struct tcp_sock *tcp;
  14. int copied;
  15. if (!skb || !skb->len || !sk_is_tcp(sk))
  16. return;
  17. if (skb_bpf_strparser(skb))
  18. return;
  19. tcp = tcp_sk(sk);
  20. copied = tcp->copied_seq + skb->len;
  21. WRITE_ONCE(tcp->copied_seq, copied);
  22. tcp_rcv_space_adjust(sk);
  23. __tcp_cleanup_rbuf(sk, skb->len);
  24. }
  25. static int bpf_tcp_ingress(struct sock *sk, struct sk_psock *psock,
  26. struct sk_msg *msg, u32 apply_bytes)
  27. {
  28. bool apply = apply_bytes;
  29. struct scatterlist *sge;
  30. u32 size, copied = 0;
  31. struct sk_msg *tmp;
  32. int i, ret = 0;
  33. tmp = kzalloc(sizeof(*tmp), __GFP_NOWARN | GFP_KERNEL);
  34. if (unlikely(!tmp))
  35. return -ENOMEM;
  36. lock_sock(sk);
  37. tmp->sg.start = msg->sg.start;
  38. i = msg->sg.start;
  39. do {
  40. sge = sk_msg_elem(msg, i);
  41. size = (apply && apply_bytes < sge->length) ?
  42. apply_bytes : sge->length;
  43. if (!__sk_rmem_schedule(sk, size, false)) {
  44. if (!copied)
  45. ret = -ENOMEM;
  46. break;
  47. }
  48. sk_mem_charge(sk, size);
  49. atomic_add(size, &sk->sk_rmem_alloc);
  50. sk_msg_xfer(tmp, msg, i, size);
  51. copied += size;
  52. if (sge->length)
  53. get_page(sk_msg_page(tmp, i));
  54. sk_msg_iter_var_next(i);
  55. tmp->sg.end = i;
  56. if (apply) {
  57. apply_bytes -= size;
  58. if (!apply_bytes) {
  59. if (sge->length)
  60. sk_msg_iter_var_prev(i);
  61. break;
  62. }
  63. }
  64. } while (i != msg->sg.end);
  65. if (!ret) {
  66. msg->sg.start = i;
  67. if (!sk_psock_queue_msg(psock, tmp))
  68. atomic_sub(copied, &sk->sk_rmem_alloc);
  69. sk_psock_data_ready(sk, psock);
  70. } else {
  71. sk_msg_free(sk, tmp);
  72. kfree(tmp);
  73. }
  74. release_sock(sk);
  75. return ret;
  76. }
  77. static int tcp_bpf_push(struct sock *sk, struct sk_msg *msg, u32 apply_bytes,
  78. int flags, bool uncharge)
  79. {
  80. struct msghdr msghdr = {};
  81. bool apply = apply_bytes;
  82. struct scatterlist *sge;
  83. struct page *page;
  84. int size, ret = 0;
  85. u32 off;
  86. while (1) {
  87. struct bio_vec bvec;
  88. bool has_tx_ulp;
  89. sge = sk_msg_elem(msg, msg->sg.start);
  90. size = (apply && apply_bytes < sge->length) ?
  91. apply_bytes : sge->length;
  92. off = sge->offset;
  93. page = sg_page(sge);
  94. tcp_rate_check_app_limited(sk);
  95. retry:
  96. msghdr.msg_flags = flags | MSG_SPLICE_PAGES;
  97. has_tx_ulp = tls_sw_has_ctx_tx(sk);
  98. if (has_tx_ulp)
  99. msghdr.msg_flags |= MSG_SENDPAGE_NOPOLICY;
  100. if (size < sge->length && msg->sg.start != msg->sg.end)
  101. msghdr.msg_flags |= MSG_MORE;
  102. bvec_set_page(&bvec, page, size, off);
  103. iov_iter_bvec(&msghdr.msg_iter, ITER_SOURCE, &bvec, 1, size);
  104. ret = tcp_sendmsg_locked(sk, &msghdr, size);
  105. if (ret <= 0)
  106. return ret;
  107. if (apply)
  108. apply_bytes -= ret;
  109. msg->sg.size -= ret;
  110. sge->offset += ret;
  111. sge->length -= ret;
  112. if (uncharge)
  113. sk_mem_uncharge(sk, ret);
  114. if (ret != size) {
  115. size -= ret;
  116. off += ret;
  117. goto retry;
  118. }
  119. if (!sge->length) {
  120. put_page(page);
  121. sk_msg_iter_next(msg, start);
  122. sg_init_table(sge, 1);
  123. if (msg->sg.start == msg->sg.end)
  124. break;
  125. }
  126. if (apply && !apply_bytes)
  127. break;
  128. }
  129. return 0;
  130. }
  131. static int tcp_bpf_push_locked(struct sock *sk, struct sk_msg *msg,
  132. u32 apply_bytes, int flags, bool uncharge)
  133. {
  134. int ret;
  135. lock_sock(sk);
  136. ret = tcp_bpf_push(sk, msg, apply_bytes, flags, uncharge);
  137. release_sock(sk);
  138. return ret;
  139. }
  140. int tcp_bpf_sendmsg_redir(struct sock *sk, bool ingress,
  141. struct sk_msg *msg, u32 bytes, int flags)
  142. {
  143. struct sk_psock *psock = sk_psock_get(sk);
  144. int ret;
  145. if (unlikely(!psock))
  146. return -EPIPE;
  147. ret = ingress ? bpf_tcp_ingress(sk, psock, msg, bytes) :
  148. tcp_bpf_push_locked(sk, msg, bytes, flags, false);
  149. sk_psock_put(sk, psock);
  150. return ret;
  151. }
  152. EXPORT_SYMBOL_GPL(tcp_bpf_sendmsg_redir);
  153. #ifdef CONFIG_BPF_SYSCALL
  154. static int tcp_msg_wait_data(struct sock *sk, struct sk_psock *psock,
  155. long timeo)
  156. {
  157. DEFINE_WAIT_FUNC(wait, woken_wake_function);
  158. int ret = 0;
  159. if (sk->sk_shutdown & RCV_SHUTDOWN)
  160. return 1;
  161. if (!timeo)
  162. return ret;
  163. add_wait_queue(sk_sleep(sk), &wait);
  164. sk_set_bit(SOCKWQ_ASYNC_WAITDATA, sk);
  165. ret = sk_wait_event(sk, &timeo,
  166. !list_empty(&psock->ingress_msg) ||
  167. !skb_queue_empty_lockless(&sk->sk_receive_queue), &wait);
  168. sk_clear_bit(SOCKWQ_ASYNC_WAITDATA, sk);
  169. remove_wait_queue(sk_sleep(sk), &wait);
  170. return ret;
  171. }
  172. static bool is_next_msg_fin(struct sk_psock *psock)
  173. {
  174. struct scatterlist *sge;
  175. struct sk_msg *msg_rx;
  176. int i;
  177. msg_rx = sk_psock_peek_msg(psock);
  178. i = msg_rx->sg.start;
  179. sge = sk_msg_elem(msg_rx, i);
  180. if (!sge->length) {
  181. struct sk_buff *skb = msg_rx->skb;
  182. if (skb && TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN)
  183. return true;
  184. }
  185. return false;
  186. }
  187. static int tcp_bpf_recvmsg_parser(struct sock *sk,
  188. struct msghdr *msg,
  189. size_t len,
  190. int flags,
  191. int *addr_len)
  192. {
  193. int peek = flags & MSG_PEEK;
  194. struct sk_psock *psock;
  195. struct tcp_sock *tcp;
  196. int copied = 0;
  197. u32 seq;
  198. if (unlikely(flags & MSG_ERRQUEUE))
  199. return inet_recv_error(sk, msg, len, addr_len);
  200. if (!len)
  201. return 0;
  202. psock = sk_psock_get(sk);
  203. if (unlikely(!psock))
  204. return tcp_recvmsg(sk, msg, len, flags, addr_len);
  205. lock_sock(sk);
  206. tcp = tcp_sk(sk);
  207. seq = tcp->copied_seq;
  208. /* We may have received data on the sk_receive_queue pre-accept and
  209. * then we can not use read_skb in this context because we haven't
  210. * assigned a sk_socket yet so have no link to the ops. The work-around
  211. * is to check the sk_receive_queue and in these cases read skbs off
  212. * queue again. The read_skb hook is not running at this point because
  213. * of lock_sock so we avoid having multiple runners in read_skb.
  214. */
  215. if (unlikely(!skb_queue_empty(&sk->sk_receive_queue))) {
  216. tcp_data_ready(sk);
  217. /* This handles the ENOMEM errors if we both receive data
  218. * pre accept and are already under memory pressure. At least
  219. * let user know to retry.
  220. */
  221. if (unlikely(!skb_queue_empty(&sk->sk_receive_queue))) {
  222. copied = -EAGAIN;
  223. goto out;
  224. }
  225. }
  226. msg_bytes_ready:
  227. copied = sk_msg_recvmsg(sk, psock, msg, len, flags);
  228. /* The typical case for EFAULT is the socket was gracefully
  229. * shutdown with a FIN pkt. So check here the other case is
  230. * some error on copy_page_to_iter which would be unexpected.
  231. * On fin return correct return code to zero.
  232. */
  233. if (copied == -EFAULT) {
  234. bool is_fin = is_next_msg_fin(psock);
  235. if (is_fin) {
  236. copied = 0;
  237. seq++;
  238. goto out;
  239. }
  240. }
  241. seq += copied;
  242. if (!copied) {
  243. long timeo;
  244. int data;
  245. if (sock_flag(sk, SOCK_DONE))
  246. goto out;
  247. if (sk->sk_err) {
  248. copied = sock_error(sk);
  249. goto out;
  250. }
  251. if (sk->sk_shutdown & RCV_SHUTDOWN)
  252. goto out;
  253. if (sk->sk_state == TCP_CLOSE) {
  254. copied = -ENOTCONN;
  255. goto out;
  256. }
  257. timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
  258. if (!timeo) {
  259. copied = -EAGAIN;
  260. goto out;
  261. }
  262. if (signal_pending(current)) {
  263. copied = sock_intr_errno(timeo);
  264. goto out;
  265. }
  266. data = tcp_msg_wait_data(sk, psock, timeo);
  267. if (data < 0) {
  268. copied = data;
  269. goto unlock;
  270. }
  271. if (data && !sk_psock_queue_empty(psock))
  272. goto msg_bytes_ready;
  273. copied = -EAGAIN;
  274. }
  275. out:
  276. if (!peek)
  277. WRITE_ONCE(tcp->copied_seq, seq);
  278. tcp_rcv_space_adjust(sk);
  279. if (copied > 0)
  280. __tcp_cleanup_rbuf(sk, copied);
  281. unlock:
  282. release_sock(sk);
  283. sk_psock_put(sk, psock);
  284. return copied;
  285. }
  286. static int tcp_bpf_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
  287. int flags, int *addr_len)
  288. {
  289. struct sk_psock *psock;
  290. int copied, ret;
  291. if (unlikely(flags & MSG_ERRQUEUE))
  292. return inet_recv_error(sk, msg, len, addr_len);
  293. if (!len)
  294. return 0;
  295. psock = sk_psock_get(sk);
  296. if (unlikely(!psock))
  297. return tcp_recvmsg(sk, msg, len, flags, addr_len);
  298. if (!skb_queue_empty(&sk->sk_receive_queue) &&
  299. sk_psock_queue_empty(psock)) {
  300. sk_psock_put(sk, psock);
  301. return tcp_recvmsg(sk, msg, len, flags, addr_len);
  302. }
  303. lock_sock(sk);
  304. msg_bytes_ready:
  305. copied = sk_msg_recvmsg(sk, psock, msg, len, flags);
  306. if (!copied) {
  307. long timeo;
  308. int data;
  309. timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
  310. data = tcp_msg_wait_data(sk, psock, timeo);
  311. if (data < 0) {
  312. ret = data;
  313. goto unlock;
  314. }
  315. if (data) {
  316. if (!sk_psock_queue_empty(psock))
  317. goto msg_bytes_ready;
  318. release_sock(sk);
  319. sk_psock_put(sk, psock);
  320. return tcp_recvmsg(sk, msg, len, flags, addr_len);
  321. }
  322. copied = -EAGAIN;
  323. }
  324. ret = copied;
  325. unlock:
  326. release_sock(sk);
  327. sk_psock_put(sk, psock);
  328. return ret;
  329. }
  330. static int tcp_bpf_send_verdict(struct sock *sk, struct sk_psock *psock,
  331. struct sk_msg *msg, int *copied, int flags)
  332. {
  333. bool cork = false, enospc = sk_msg_full(msg), redir_ingress;
  334. struct sock *sk_redir;
  335. u32 tosend, origsize, sent, delta = 0;
  336. u32 eval;
  337. int ret;
  338. more_data:
  339. if (psock->eval == __SK_NONE) {
  340. /* Track delta in msg size to add/subtract it on SK_DROP from
  341. * returned to user copied size. This ensures user doesn't
  342. * get a positive return code with msg_cut_data and SK_DROP
  343. * verdict.
  344. */
  345. delta = msg->sg.size;
  346. psock->eval = sk_psock_msg_verdict(sk, psock, msg);
  347. delta -= msg->sg.size;
  348. }
  349. if (msg->cork_bytes &&
  350. msg->cork_bytes > msg->sg.size && !enospc) {
  351. psock->cork_bytes = msg->cork_bytes - msg->sg.size;
  352. if (!psock->cork) {
  353. psock->cork = kzalloc(sizeof(*psock->cork),
  354. GFP_ATOMIC | __GFP_NOWARN);
  355. if (!psock->cork) {
  356. sk_msg_free(sk, msg);
  357. *copied = 0;
  358. return -ENOMEM;
  359. }
  360. }
  361. memcpy(psock->cork, msg, sizeof(*msg));
  362. return 0;
  363. }
  364. tosend = msg->sg.size;
  365. if (psock->apply_bytes && psock->apply_bytes < tosend)
  366. tosend = psock->apply_bytes;
  367. eval = __SK_NONE;
  368. switch (psock->eval) {
  369. case __SK_PASS:
  370. ret = tcp_bpf_push(sk, msg, tosend, flags, true);
  371. if (unlikely(ret)) {
  372. *copied -= sk_msg_free(sk, msg);
  373. break;
  374. }
  375. sk_msg_apply_bytes(psock, tosend);
  376. break;
  377. case __SK_REDIRECT:
  378. redir_ingress = psock->redir_ingress;
  379. sk_redir = psock->sk_redir;
  380. sk_msg_apply_bytes(psock, tosend);
  381. if (!psock->apply_bytes) {
  382. /* Clean up before releasing the sock lock. */
  383. eval = psock->eval;
  384. psock->eval = __SK_NONE;
  385. psock->sk_redir = NULL;
  386. }
  387. if (psock->cork) {
  388. cork = true;
  389. psock->cork = NULL;
  390. }
  391. release_sock(sk);
  392. origsize = msg->sg.size;
  393. ret = tcp_bpf_sendmsg_redir(sk_redir, redir_ingress,
  394. msg, tosend, flags);
  395. sent = origsize - msg->sg.size;
  396. if (eval == __SK_REDIRECT)
  397. sock_put(sk_redir);
  398. lock_sock(sk);
  399. sk_mem_uncharge(sk, sent);
  400. if (unlikely(ret < 0)) {
  401. int free = sk_msg_free(sk, msg);
  402. if (!cork)
  403. *copied -= free;
  404. }
  405. if (cork) {
  406. sk_msg_free(sk, msg);
  407. kfree(msg);
  408. msg = NULL;
  409. ret = 0;
  410. }
  411. break;
  412. case __SK_DROP:
  413. default:
  414. sk_msg_free(sk, msg);
  415. sk_msg_apply_bytes(psock, tosend);
  416. *copied -= (tosend + delta);
  417. return -EACCES;
  418. }
  419. if (likely(!ret)) {
  420. if (!psock->apply_bytes) {
  421. psock->eval = __SK_NONE;
  422. if (psock->sk_redir) {
  423. sock_put(psock->sk_redir);
  424. psock->sk_redir = NULL;
  425. }
  426. }
  427. if (msg &&
  428. msg->sg.data[msg->sg.start].page_link &&
  429. msg->sg.data[msg->sg.start].length)
  430. goto more_data;
  431. }
  432. return ret;
  433. }
  434. static int tcp_bpf_sendmsg(struct sock *sk, struct msghdr *msg, size_t size)
  435. {
  436. struct sk_msg tmp, *msg_tx = NULL;
  437. int copied = 0, err = 0;
  438. struct sk_psock *psock;
  439. long timeo;
  440. int flags;
  441. /* Don't let internal flags through */
  442. flags = (msg->msg_flags & ~MSG_SENDPAGE_DECRYPTED);
  443. flags |= MSG_NO_SHARED_FRAGS;
  444. psock = sk_psock_get(sk);
  445. if (unlikely(!psock))
  446. return tcp_sendmsg(sk, msg, size);
  447. lock_sock(sk);
  448. timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
  449. while (msg_data_left(msg)) {
  450. bool enospc = false;
  451. u32 copy, osize;
  452. if (sk->sk_err) {
  453. err = -sk->sk_err;
  454. goto out_err;
  455. }
  456. copy = msg_data_left(msg);
  457. if (!sk_stream_memory_free(sk))
  458. goto wait_for_sndbuf;
  459. if (psock->cork) {
  460. msg_tx = psock->cork;
  461. } else {
  462. msg_tx = &tmp;
  463. sk_msg_init(msg_tx);
  464. }
  465. osize = msg_tx->sg.size;
  466. err = sk_msg_alloc(sk, msg_tx, msg_tx->sg.size + copy, msg_tx->sg.end - 1);
  467. if (err) {
  468. if (err != -ENOSPC)
  469. goto wait_for_memory;
  470. enospc = true;
  471. copy = msg_tx->sg.size - osize;
  472. }
  473. err = sk_msg_memcopy_from_iter(sk, &msg->msg_iter, msg_tx,
  474. copy);
  475. if (err < 0) {
  476. sk_msg_trim(sk, msg_tx, osize);
  477. goto out_err;
  478. }
  479. copied += copy;
  480. if (psock->cork_bytes) {
  481. if (size > psock->cork_bytes)
  482. psock->cork_bytes = 0;
  483. else
  484. psock->cork_bytes -= size;
  485. if (psock->cork_bytes && !enospc)
  486. goto out_err;
  487. /* All cork bytes are accounted, rerun the prog. */
  488. psock->eval = __SK_NONE;
  489. psock->cork_bytes = 0;
  490. }
  491. err = tcp_bpf_send_verdict(sk, psock, msg_tx, &copied, flags);
  492. if (unlikely(err < 0))
  493. goto out_err;
  494. continue;
  495. wait_for_sndbuf:
  496. set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
  497. wait_for_memory:
  498. err = sk_stream_wait_memory(sk, &timeo);
  499. if (err) {
  500. if (msg_tx && msg_tx != psock->cork)
  501. sk_msg_free(sk, msg_tx);
  502. goto out_err;
  503. }
  504. }
  505. out_err:
  506. if (err < 0)
  507. err = sk_stream_error(sk, msg->msg_flags, err);
  508. release_sock(sk);
  509. sk_psock_put(sk, psock);
  510. return copied > 0 ? copied : err;
  511. }
  512. enum {
  513. TCP_BPF_IPV4,
  514. TCP_BPF_IPV6,
  515. TCP_BPF_NUM_PROTS,
  516. };
  517. enum {
  518. TCP_BPF_BASE,
  519. TCP_BPF_TX,
  520. TCP_BPF_RX,
  521. TCP_BPF_TXRX,
  522. TCP_BPF_NUM_CFGS,
  523. };
  524. static struct proto *tcpv6_prot_saved __read_mostly;
  525. static DEFINE_SPINLOCK(tcpv6_prot_lock);
  526. static struct proto tcp_bpf_prots[TCP_BPF_NUM_PROTS][TCP_BPF_NUM_CFGS];
  527. static void tcp_bpf_rebuild_protos(struct proto prot[TCP_BPF_NUM_CFGS],
  528. struct proto *base)
  529. {
  530. prot[TCP_BPF_BASE] = *base;
  531. prot[TCP_BPF_BASE].destroy = sock_map_destroy;
  532. prot[TCP_BPF_BASE].close = sock_map_close;
  533. prot[TCP_BPF_BASE].recvmsg = tcp_bpf_recvmsg;
  534. prot[TCP_BPF_BASE].sock_is_readable = sk_msg_is_readable;
  535. prot[TCP_BPF_TX] = prot[TCP_BPF_BASE];
  536. prot[TCP_BPF_TX].sendmsg = tcp_bpf_sendmsg;
  537. prot[TCP_BPF_RX] = prot[TCP_BPF_BASE];
  538. prot[TCP_BPF_RX].recvmsg = tcp_bpf_recvmsg_parser;
  539. prot[TCP_BPF_TXRX] = prot[TCP_BPF_TX];
  540. prot[TCP_BPF_TXRX].recvmsg = tcp_bpf_recvmsg_parser;
  541. }
  542. static void tcp_bpf_check_v6_needs_rebuild(struct proto *ops)
  543. {
  544. if (unlikely(ops != smp_load_acquire(&tcpv6_prot_saved))) {
  545. spin_lock_bh(&tcpv6_prot_lock);
  546. if (likely(ops != tcpv6_prot_saved)) {
  547. tcp_bpf_rebuild_protos(tcp_bpf_prots[TCP_BPF_IPV6], ops);
  548. smp_store_release(&tcpv6_prot_saved, ops);
  549. }
  550. spin_unlock_bh(&tcpv6_prot_lock);
  551. }
  552. }
  553. static int __init tcp_bpf_v4_build_proto(void)
  554. {
  555. tcp_bpf_rebuild_protos(tcp_bpf_prots[TCP_BPF_IPV4], &tcp_prot);
  556. return 0;
  557. }
  558. late_initcall(tcp_bpf_v4_build_proto);
  559. static int tcp_bpf_assert_proto_ops(struct proto *ops)
  560. {
  561. /* In order to avoid retpoline, we make assumptions when we call
  562. * into ops if e.g. a psock is not present. Make sure they are
  563. * indeed valid assumptions.
  564. */
  565. return ops->recvmsg == tcp_recvmsg &&
  566. ops->sendmsg == tcp_sendmsg ? 0 : -ENOTSUPP;
  567. }
  568. #if IS_ENABLED(CONFIG_BPF_STREAM_PARSER)
  569. int tcp_bpf_strp_read_sock(struct strparser *strp, read_descriptor_t *desc,
  570. sk_read_actor_t recv_actor)
  571. {
  572. struct sock *sk = strp->sk;
  573. struct sk_psock *psock;
  574. struct tcp_sock *tp;
  575. int copied = 0;
  576. tp = tcp_sk(sk);
  577. rcu_read_lock();
  578. psock = sk_psock(sk);
  579. if (WARN_ON_ONCE(!psock)) {
  580. desc->error = -EINVAL;
  581. goto out;
  582. }
  583. psock->ingress_bytes = 0;
  584. copied = tcp_read_sock_noack(sk, desc, recv_actor, true,
  585. &psock->copied_seq);
  586. if (copied < 0)
  587. goto out;
  588. /* recv_actor may redirect skb to another socket (SK_REDIRECT) or
  589. * just put skb into ingress queue of current socket (SK_PASS).
  590. * For SK_REDIRECT, we need to ack the frame immediately but for
  591. * SK_PASS, we want to delay the ack until tcp_bpf_recvmsg_parser().
  592. */
  593. tp->copied_seq = psock->copied_seq - psock->ingress_bytes;
  594. tcp_rcv_space_adjust(sk);
  595. __tcp_cleanup_rbuf(sk, copied - psock->ingress_bytes);
  596. out:
  597. rcu_read_unlock();
  598. return copied;
  599. }
  600. #endif /* CONFIG_BPF_STREAM_PARSER */
  601. int tcp_bpf_update_proto(struct sock *sk, struct sk_psock *psock, bool restore)
  602. {
  603. int family = sk->sk_family == AF_INET6 ? TCP_BPF_IPV6 : TCP_BPF_IPV4;
  604. int config = psock->progs.msg_parser ? TCP_BPF_TX : TCP_BPF_BASE;
  605. if (psock->progs.stream_verdict || psock->progs.skb_verdict) {
  606. config = (config == TCP_BPF_TX) ? TCP_BPF_TXRX : TCP_BPF_RX;
  607. }
  608. if (restore) {
  609. if (inet_csk_has_ulp(sk)) {
  610. /* TLS does not have an unhash proto in SW cases,
  611. * but we need to ensure we stop using the sock_map
  612. * unhash routine because the associated psock is being
  613. * removed. So use the original unhash handler.
  614. */
  615. WRITE_ONCE(sk->sk_prot->unhash, psock->saved_unhash);
  616. tcp_update_ulp(sk, psock->sk_proto, psock->saved_write_space);
  617. } else {
  618. sk->sk_write_space = psock->saved_write_space;
  619. /* Pairs with lockless read in sk_clone_lock() */
  620. sock_replace_proto(sk, psock->sk_proto);
  621. }
  622. return 0;
  623. }
  624. if (sk->sk_family == AF_INET6) {
  625. if (tcp_bpf_assert_proto_ops(psock->sk_proto))
  626. return -EINVAL;
  627. tcp_bpf_check_v6_needs_rebuild(psock->sk_proto);
  628. }
  629. /* Pairs with lockless read in sk_clone_lock() */
  630. sock_replace_proto(sk, &tcp_bpf_prots[family][config]);
  631. return 0;
  632. }
  633. EXPORT_SYMBOL_GPL(tcp_bpf_update_proto);
  634. /* If a child got cloned from a listening socket that had tcp_bpf
  635. * protocol callbacks installed, we need to restore the callbacks to
  636. * the default ones because the child does not inherit the psock state
  637. * that tcp_bpf callbacks expect.
  638. */
  639. void tcp_bpf_clone(const struct sock *sk, struct sock *newsk)
  640. {
  641. struct proto *prot = newsk->sk_prot;
  642. if (is_insidevar(prot, tcp_bpf_prots))
  643. newsk->sk_prot = sk->sk_prot_creator;
  644. }
  645. #endif /* CONFIG_BPF_SYSCALL */