PageRenderTime 68ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/net/rxrpc/recvmsg.c

https://github.com/gby/linux
C | 705 lines | 510 code | 93 blank | 102 comment | 85 complexity | aec4b12d8d4305a1ceed5c9da0fc960f MD5 | raw file
  1. /* RxRPC recvmsg() implementation
  2. *
  3. * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
  4. * Written by David Howells (dhowells@redhat.com)
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the License, or (at your option) any later version.
  10. */
  11. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  12. #include <linux/net.h>
  13. #include <linux/skbuff.h>
  14. #include <linux/export.h>
  15. #include <linux/sched/signal.h>
  16. #include <net/sock.h>
  17. #include <net/af_rxrpc.h>
  18. #include "ar-internal.h"
  19. /*
  20. * Post a call for attention by the socket or kernel service. Further
  21. * notifications are suppressed by putting recvmsg_link on a dummy queue.
  22. */
  23. void rxrpc_notify_socket(struct rxrpc_call *call)
  24. {
  25. struct rxrpc_sock *rx;
  26. struct sock *sk;
  27. _enter("%d", call->debug_id);
  28. if (!list_empty(&call->recvmsg_link))
  29. return;
  30. rcu_read_lock();
  31. rx = rcu_dereference(call->socket);
  32. sk = &rx->sk;
  33. if (rx && sk->sk_state < RXRPC_CLOSE) {
  34. if (call->notify_rx) {
  35. call->notify_rx(sk, call, call->user_call_ID);
  36. } else {
  37. write_lock_bh(&rx->recvmsg_lock);
  38. if (list_empty(&call->recvmsg_link)) {
  39. rxrpc_get_call(call, rxrpc_call_got);
  40. list_add_tail(&call->recvmsg_link, &rx->recvmsg_q);
  41. }
  42. write_unlock_bh(&rx->recvmsg_lock);
  43. if (!sock_flag(sk, SOCK_DEAD)) {
  44. _debug("call %ps", sk->sk_data_ready);
  45. sk->sk_data_ready(sk);
  46. }
  47. }
  48. }
  49. rcu_read_unlock();
  50. _leave("");
  51. }
  52. /*
  53. * Pass a call terminating message to userspace.
  54. */
  55. static int rxrpc_recvmsg_term(struct rxrpc_call *call, struct msghdr *msg)
  56. {
  57. u32 tmp = 0;
  58. int ret;
  59. switch (call->completion) {
  60. case RXRPC_CALL_SUCCEEDED:
  61. ret = 0;
  62. if (rxrpc_is_service_call(call))
  63. ret = put_cmsg(msg, SOL_RXRPC, RXRPC_ACK, 0, &tmp);
  64. break;
  65. case RXRPC_CALL_REMOTELY_ABORTED:
  66. tmp = call->abort_code;
  67. ret = put_cmsg(msg, SOL_RXRPC, RXRPC_ABORT, 4, &tmp);
  68. break;
  69. case RXRPC_CALL_LOCALLY_ABORTED:
  70. tmp = call->abort_code;
  71. ret = put_cmsg(msg, SOL_RXRPC, RXRPC_ABORT, 4, &tmp);
  72. break;
  73. case RXRPC_CALL_NETWORK_ERROR:
  74. tmp = -call->error;
  75. ret = put_cmsg(msg, SOL_RXRPC, RXRPC_NET_ERROR, 4, &tmp);
  76. break;
  77. case RXRPC_CALL_LOCAL_ERROR:
  78. tmp = -call->error;
  79. ret = put_cmsg(msg, SOL_RXRPC, RXRPC_LOCAL_ERROR, 4, &tmp);
  80. break;
  81. default:
  82. pr_err("Invalid terminal call state %u\n", call->state);
  83. BUG();
  84. break;
  85. }
  86. trace_rxrpc_recvmsg(call, rxrpc_recvmsg_terminal, call->rx_hard_ack,
  87. call->rx_pkt_offset, call->rx_pkt_len, ret);
  88. return ret;
  89. }
  90. /*
  91. * Pass back notification of a new call. The call is added to the
  92. * to-be-accepted list. This means that the next call to be accepted might not
  93. * be the last call seen awaiting acceptance, but unless we leave this on the
  94. * front of the queue and block all other messages until someone gives us a
  95. * user_ID for it, there's not a lot we can do.
  96. */
  97. static int rxrpc_recvmsg_new_call(struct rxrpc_sock *rx,
  98. struct rxrpc_call *call,
  99. struct msghdr *msg, int flags)
  100. {
  101. int tmp = 0, ret;
  102. ret = put_cmsg(msg, SOL_RXRPC, RXRPC_NEW_CALL, 0, &tmp);
  103. if (ret == 0 && !(flags & MSG_PEEK)) {
  104. _debug("to be accepted");
  105. write_lock_bh(&rx->recvmsg_lock);
  106. list_del_init(&call->recvmsg_link);
  107. write_unlock_bh(&rx->recvmsg_lock);
  108. rxrpc_get_call(call, rxrpc_call_got);
  109. write_lock(&rx->call_lock);
  110. list_add_tail(&call->accept_link, &rx->to_be_accepted);
  111. write_unlock(&rx->call_lock);
  112. }
  113. trace_rxrpc_recvmsg(call, rxrpc_recvmsg_to_be_accepted, 1, 0, 0, ret);
  114. return ret;
  115. }
  116. /*
  117. * End the packet reception phase.
  118. */
  119. static void rxrpc_end_rx_phase(struct rxrpc_call *call, rxrpc_serial_t serial)
  120. {
  121. _enter("%d,%s", call->debug_id, rxrpc_call_states[call->state]);
  122. trace_rxrpc_receive(call, rxrpc_receive_end, 0, call->rx_top);
  123. ASSERTCMP(call->rx_hard_ack, ==, call->rx_top);
  124. if (call->state == RXRPC_CALL_CLIENT_RECV_REPLY) {
  125. rxrpc_propose_ACK(call, RXRPC_ACK_IDLE, 0, serial, true, false,
  126. rxrpc_propose_ack_terminal_ack);
  127. rxrpc_send_ack_packet(call, false);
  128. }
  129. write_lock_bh(&call->state_lock);
  130. switch (call->state) {
  131. case RXRPC_CALL_CLIENT_RECV_REPLY:
  132. __rxrpc_call_completed(call);
  133. write_unlock_bh(&call->state_lock);
  134. break;
  135. case RXRPC_CALL_SERVER_RECV_REQUEST:
  136. call->tx_phase = true;
  137. call->state = RXRPC_CALL_SERVER_ACK_REQUEST;
  138. call->ack_at = call->expire_at;
  139. write_unlock_bh(&call->state_lock);
  140. rxrpc_propose_ACK(call, RXRPC_ACK_DELAY, 0, serial, false, true,
  141. rxrpc_propose_ack_processing_op);
  142. break;
  143. default:
  144. write_unlock_bh(&call->state_lock);
  145. break;
  146. }
  147. }
  148. /*
  149. * Discard a packet we've used up and advance the Rx window by one.
  150. */
  151. static void rxrpc_rotate_rx_window(struct rxrpc_call *call)
  152. {
  153. struct rxrpc_skb_priv *sp;
  154. struct sk_buff *skb;
  155. rxrpc_serial_t serial;
  156. rxrpc_seq_t hard_ack, top;
  157. u8 flags;
  158. int ix;
  159. _enter("%d", call->debug_id);
  160. hard_ack = call->rx_hard_ack;
  161. top = smp_load_acquire(&call->rx_top);
  162. ASSERT(before(hard_ack, top));
  163. hard_ack++;
  164. ix = hard_ack & RXRPC_RXTX_BUFF_MASK;
  165. skb = call->rxtx_buffer[ix];
  166. rxrpc_see_skb(skb, rxrpc_skb_rx_rotated);
  167. sp = rxrpc_skb(skb);
  168. flags = sp->hdr.flags;
  169. serial = sp->hdr.serial;
  170. if (call->rxtx_annotations[ix] & RXRPC_RX_ANNO_JUMBO)
  171. serial += (call->rxtx_annotations[ix] & RXRPC_RX_ANNO_JUMBO) - 1;
  172. call->rxtx_buffer[ix] = NULL;
  173. call->rxtx_annotations[ix] = 0;
  174. /* Barrier against rxrpc_input_data(). */
  175. smp_store_release(&call->rx_hard_ack, hard_ack);
  176. rxrpc_free_skb(skb, rxrpc_skb_rx_freed);
  177. _debug("%u,%u,%02x", hard_ack, top, flags);
  178. trace_rxrpc_receive(call, rxrpc_receive_rotate, serial, hard_ack);
  179. if (flags & RXRPC_LAST_PACKET) {
  180. rxrpc_end_rx_phase(call, serial);
  181. } else {
  182. /* Check to see if there's an ACK that needs sending. */
  183. if (after_eq(hard_ack, call->ackr_consumed + 2) ||
  184. after_eq(top, call->ackr_seen + 2) ||
  185. (hard_ack == top && after(hard_ack, call->ackr_consumed)))
  186. rxrpc_propose_ACK(call, RXRPC_ACK_DELAY, 0, serial,
  187. true, false,
  188. rxrpc_propose_ack_rotate_rx);
  189. if (call->ackr_reason)
  190. rxrpc_send_ack_packet(call, false);
  191. }
  192. }
  193. /*
  194. * Decrypt and verify a (sub)packet. The packet's length may be changed due to
  195. * padding, but if this is the case, the packet length will be resident in the
  196. * socket buffer. Note that we can't modify the master skb info as the skb may
  197. * be the home to multiple subpackets.
  198. */
  199. static int rxrpc_verify_packet(struct rxrpc_call *call, struct sk_buff *skb,
  200. u8 annotation,
  201. unsigned int offset, unsigned int len)
  202. {
  203. struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
  204. rxrpc_seq_t seq = sp->hdr.seq;
  205. u16 cksum = sp->hdr.cksum;
  206. _enter("");
  207. /* For all but the head jumbo subpacket, the security checksum is in a
  208. * jumbo header immediately prior to the data.
  209. */
  210. if ((annotation & RXRPC_RX_ANNO_JUMBO) > 1) {
  211. __be16 tmp;
  212. if (skb_copy_bits(skb, offset - 2, &tmp, 2) < 0)
  213. BUG();
  214. cksum = ntohs(tmp);
  215. seq += (annotation & RXRPC_RX_ANNO_JUMBO) - 1;
  216. }
  217. return call->conn->security->verify_packet(call, skb, offset, len,
  218. seq, cksum);
  219. }
  220. /*
  221. * Locate the data within a packet. This is complicated by:
  222. *
  223. * (1) An skb may contain a jumbo packet - so we have to find the appropriate
  224. * subpacket.
  225. *
  226. * (2) The (sub)packets may be encrypted and, if so, the encrypted portion
  227. * contains an extra header which includes the true length of the data,
  228. * excluding any encrypted padding.
  229. */
  230. static int rxrpc_locate_data(struct rxrpc_call *call, struct sk_buff *skb,
  231. u8 *_annotation,
  232. unsigned int *_offset, unsigned int *_len)
  233. {
  234. unsigned int offset = sizeof(struct rxrpc_wire_header);
  235. unsigned int len = *_len;
  236. int ret;
  237. u8 annotation = *_annotation;
  238. /* Locate the subpacket */
  239. len = skb->len - offset;
  240. if ((annotation & RXRPC_RX_ANNO_JUMBO) > 0) {
  241. offset += (((annotation & RXRPC_RX_ANNO_JUMBO) - 1) *
  242. RXRPC_JUMBO_SUBPKTLEN);
  243. len = (annotation & RXRPC_RX_ANNO_JLAST) ?
  244. skb->len - offset : RXRPC_JUMBO_SUBPKTLEN;
  245. }
  246. if (!(annotation & RXRPC_RX_ANNO_VERIFIED)) {
  247. ret = rxrpc_verify_packet(call, skb, annotation, offset, len);
  248. if (ret < 0)
  249. return ret;
  250. *_annotation |= RXRPC_RX_ANNO_VERIFIED;
  251. }
  252. *_offset = offset;
  253. *_len = len;
  254. call->conn->security->locate_data(call, skb, _offset, _len);
  255. return 0;
  256. }
  257. /*
  258. * Deliver messages to a call. This keeps processing packets until the buffer
  259. * is filled and we find either more DATA (returns 0) or the end of the DATA
  260. * (returns 1). If more packets are required, it returns -EAGAIN.
  261. */
  262. static int rxrpc_recvmsg_data(struct socket *sock, struct rxrpc_call *call,
  263. struct msghdr *msg, struct iov_iter *iter,
  264. size_t len, int flags, size_t *_offset)
  265. {
  266. struct rxrpc_skb_priv *sp;
  267. struct sk_buff *skb;
  268. rxrpc_seq_t hard_ack, top, seq;
  269. size_t remain;
  270. bool last;
  271. unsigned int rx_pkt_offset, rx_pkt_len;
  272. int ix, copy, ret = -EAGAIN, ret2;
  273. rx_pkt_offset = call->rx_pkt_offset;
  274. rx_pkt_len = call->rx_pkt_len;
  275. if (call->state >= RXRPC_CALL_SERVER_ACK_REQUEST) {
  276. seq = call->rx_hard_ack;
  277. ret = 1;
  278. goto done;
  279. }
  280. /* Barriers against rxrpc_input_data(). */
  281. hard_ack = call->rx_hard_ack;
  282. seq = hard_ack + 1;
  283. while (top = smp_load_acquire(&call->rx_top),
  284. before_eq(seq, top)
  285. ) {
  286. ix = seq & RXRPC_RXTX_BUFF_MASK;
  287. skb = call->rxtx_buffer[ix];
  288. if (!skb) {
  289. trace_rxrpc_recvmsg(call, rxrpc_recvmsg_hole, seq,
  290. rx_pkt_offset, rx_pkt_len, 0);
  291. break;
  292. }
  293. smp_rmb();
  294. rxrpc_see_skb(skb, rxrpc_skb_rx_seen);
  295. sp = rxrpc_skb(skb);
  296. if (!(flags & MSG_PEEK))
  297. trace_rxrpc_receive(call, rxrpc_receive_front,
  298. sp->hdr.serial, seq);
  299. if (msg)
  300. sock_recv_timestamp(msg, sock->sk, skb);
  301. if (rx_pkt_offset == 0) {
  302. ret2 = rxrpc_locate_data(call, skb,
  303. &call->rxtx_annotations[ix],
  304. &rx_pkt_offset, &rx_pkt_len);
  305. trace_rxrpc_recvmsg(call, rxrpc_recvmsg_next, seq,
  306. rx_pkt_offset, rx_pkt_len, ret2);
  307. if (ret2 < 0) {
  308. ret = ret2;
  309. goto out;
  310. }
  311. } else {
  312. trace_rxrpc_recvmsg(call, rxrpc_recvmsg_cont, seq,
  313. rx_pkt_offset, rx_pkt_len, 0);
  314. }
  315. /* We have to handle short, empty and used-up DATA packets. */
  316. remain = len - *_offset;
  317. copy = rx_pkt_len;
  318. if (copy > remain)
  319. copy = remain;
  320. if (copy > 0) {
  321. ret2 = skb_copy_datagram_iter(skb, rx_pkt_offset, iter,
  322. copy);
  323. if (ret2 < 0) {
  324. ret = ret2;
  325. goto out;
  326. }
  327. /* handle piecemeal consumption of data packets */
  328. rx_pkt_offset += copy;
  329. rx_pkt_len -= copy;
  330. *_offset += copy;
  331. }
  332. if (rx_pkt_len > 0) {
  333. trace_rxrpc_recvmsg(call, rxrpc_recvmsg_full, seq,
  334. rx_pkt_offset, rx_pkt_len, 0);
  335. ASSERTCMP(*_offset, ==, len);
  336. ret = 0;
  337. break;
  338. }
  339. /* The whole packet has been transferred. */
  340. last = sp->hdr.flags & RXRPC_LAST_PACKET;
  341. if (!(flags & MSG_PEEK))
  342. rxrpc_rotate_rx_window(call);
  343. rx_pkt_offset = 0;
  344. rx_pkt_len = 0;
  345. if (last) {
  346. ASSERTCMP(seq, ==, READ_ONCE(call->rx_top));
  347. ret = 1;
  348. goto out;
  349. }
  350. seq++;
  351. }
  352. out:
  353. if (!(flags & MSG_PEEK)) {
  354. call->rx_pkt_offset = rx_pkt_offset;
  355. call->rx_pkt_len = rx_pkt_len;
  356. }
  357. done:
  358. trace_rxrpc_recvmsg(call, rxrpc_recvmsg_data_return, seq,
  359. rx_pkt_offset, rx_pkt_len, ret);
  360. return ret;
  361. }
  362. /*
  363. * Receive a message from an RxRPC socket
  364. * - we need to be careful about two or more threads calling recvmsg
  365. * simultaneously
  366. */
  367. int rxrpc_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
  368. int flags)
  369. {
  370. struct rxrpc_call *call;
  371. struct rxrpc_sock *rx = rxrpc_sk(sock->sk);
  372. struct list_head *l;
  373. size_t copied = 0;
  374. long timeo;
  375. int ret;
  376. DEFINE_WAIT(wait);
  377. trace_rxrpc_recvmsg(NULL, rxrpc_recvmsg_enter, 0, 0, 0, 0);
  378. if (flags & (MSG_OOB | MSG_TRUNC))
  379. return -EOPNOTSUPP;
  380. timeo = sock_rcvtimeo(&rx->sk, flags & MSG_DONTWAIT);
  381. try_again:
  382. lock_sock(&rx->sk);
  383. /* Return immediately if a client socket has no outstanding calls */
  384. if (RB_EMPTY_ROOT(&rx->calls) &&
  385. list_empty(&rx->recvmsg_q) &&
  386. rx->sk.sk_state != RXRPC_SERVER_LISTENING) {
  387. release_sock(&rx->sk);
  388. return -ENODATA;
  389. }
  390. if (list_empty(&rx->recvmsg_q)) {
  391. ret = -EWOULDBLOCK;
  392. if (timeo == 0) {
  393. call = NULL;
  394. goto error_no_call;
  395. }
  396. release_sock(&rx->sk);
  397. /* Wait for something to happen */
  398. prepare_to_wait_exclusive(sk_sleep(&rx->sk), &wait,
  399. TASK_INTERRUPTIBLE);
  400. ret = sock_error(&rx->sk);
  401. if (ret)
  402. goto wait_error;
  403. if (list_empty(&rx->recvmsg_q)) {
  404. if (signal_pending(current))
  405. goto wait_interrupted;
  406. trace_rxrpc_recvmsg(NULL, rxrpc_recvmsg_wait,
  407. 0, 0, 0, 0);
  408. timeo = schedule_timeout(timeo);
  409. }
  410. finish_wait(sk_sleep(&rx->sk), &wait);
  411. goto try_again;
  412. }
  413. /* Find the next call and dequeue it if we're not just peeking. If we
  414. * do dequeue it, that comes with a ref that we will need to release.
  415. */
  416. write_lock_bh(&rx->recvmsg_lock);
  417. l = rx->recvmsg_q.next;
  418. call = list_entry(l, struct rxrpc_call, recvmsg_link);
  419. if (!(flags & MSG_PEEK))
  420. list_del_init(&call->recvmsg_link);
  421. else
  422. rxrpc_get_call(call, rxrpc_call_got);
  423. write_unlock_bh(&rx->recvmsg_lock);
  424. trace_rxrpc_recvmsg(call, rxrpc_recvmsg_dequeue, 0, 0, 0, 0);
  425. /* We're going to drop the socket lock, so we need to lock the call
  426. * against interference by sendmsg.
  427. */
  428. if (!mutex_trylock(&call->user_mutex)) {
  429. ret = -EWOULDBLOCK;
  430. if (flags & MSG_DONTWAIT)
  431. goto error_requeue_call;
  432. ret = -ERESTARTSYS;
  433. if (mutex_lock_interruptible(&call->user_mutex) < 0)
  434. goto error_requeue_call;
  435. }
  436. release_sock(&rx->sk);
  437. if (test_bit(RXRPC_CALL_RELEASED, &call->flags))
  438. BUG();
  439. if (test_bit(RXRPC_CALL_HAS_USERID, &call->flags)) {
  440. if (flags & MSG_CMSG_COMPAT) {
  441. unsigned int id32 = call->user_call_ID;
  442. ret = put_cmsg(msg, SOL_RXRPC, RXRPC_USER_CALL_ID,
  443. sizeof(unsigned int), &id32);
  444. } else {
  445. ret = put_cmsg(msg, SOL_RXRPC, RXRPC_USER_CALL_ID,
  446. sizeof(unsigned long),
  447. &call->user_call_ID);
  448. }
  449. if (ret < 0)
  450. goto error_unlock_call;
  451. }
  452. if (msg->msg_name) {
  453. struct sockaddr_rxrpc *srx = msg->msg_name;
  454. size_t len = sizeof(call->peer->srx);
  455. memcpy(msg->msg_name, &call->peer->srx, len);
  456. srx->srx_service = call->service_id;
  457. msg->msg_namelen = len;
  458. }
  459. switch (READ_ONCE(call->state)) {
  460. case RXRPC_CALL_SERVER_ACCEPTING:
  461. ret = rxrpc_recvmsg_new_call(rx, call, msg, flags);
  462. break;
  463. case RXRPC_CALL_CLIENT_RECV_REPLY:
  464. case RXRPC_CALL_SERVER_RECV_REQUEST:
  465. case RXRPC_CALL_SERVER_ACK_REQUEST:
  466. ret = rxrpc_recvmsg_data(sock, call, msg, &msg->msg_iter, len,
  467. flags, &copied);
  468. if (ret == -EAGAIN)
  469. ret = 0;
  470. if (after(call->rx_top, call->rx_hard_ack) &&
  471. call->rxtx_buffer[(call->rx_hard_ack + 1) & RXRPC_RXTX_BUFF_MASK])
  472. rxrpc_notify_socket(call);
  473. break;
  474. default:
  475. ret = 0;
  476. break;
  477. }
  478. if (ret < 0)
  479. goto error_unlock_call;
  480. if (call->state == RXRPC_CALL_COMPLETE) {
  481. ret = rxrpc_recvmsg_term(call, msg);
  482. if (ret < 0)
  483. goto error_unlock_call;
  484. if (!(flags & MSG_PEEK))
  485. rxrpc_release_call(rx, call);
  486. msg->msg_flags |= MSG_EOR;
  487. ret = 1;
  488. }
  489. if (ret == 0)
  490. msg->msg_flags |= MSG_MORE;
  491. else
  492. msg->msg_flags &= ~MSG_MORE;
  493. ret = copied;
  494. error_unlock_call:
  495. mutex_unlock(&call->user_mutex);
  496. rxrpc_put_call(call, rxrpc_call_put);
  497. trace_rxrpc_recvmsg(call, rxrpc_recvmsg_return, 0, 0, 0, ret);
  498. return ret;
  499. error_requeue_call:
  500. if (!(flags & MSG_PEEK)) {
  501. write_lock_bh(&rx->recvmsg_lock);
  502. list_add(&call->recvmsg_link, &rx->recvmsg_q);
  503. write_unlock_bh(&rx->recvmsg_lock);
  504. trace_rxrpc_recvmsg(call, rxrpc_recvmsg_requeue, 0, 0, 0, 0);
  505. } else {
  506. rxrpc_put_call(call, rxrpc_call_put);
  507. }
  508. error_no_call:
  509. release_sock(&rx->sk);
  510. trace_rxrpc_recvmsg(call, rxrpc_recvmsg_return, 0, 0, 0, ret);
  511. return ret;
  512. wait_interrupted:
  513. ret = sock_intr_errno(timeo);
  514. wait_error:
  515. finish_wait(sk_sleep(&rx->sk), &wait);
  516. call = NULL;
  517. goto error_no_call;
  518. }
  519. /**
  520. * rxrpc_kernel_recv_data - Allow a kernel service to receive data/info
  521. * @sock: The socket that the call exists on
  522. * @call: The call to send data through
  523. * @buf: The buffer to receive into
  524. * @size: The size of the buffer, including data already read
  525. * @_offset: The running offset into the buffer.
  526. * @want_more: True if more data is expected to be read
  527. * @_abort: Where the abort code is stored if -ECONNABORTED is returned
  528. *
  529. * Allow a kernel service to receive data and pick up information about the
  530. * state of a call. Returns 0 if got what was asked for and there's more
  531. * available, 1 if we got what was asked for and we're at the end of the data
  532. * and -EAGAIN if we need more data.
  533. *
  534. * Note that we may return -EAGAIN to drain empty packets at the end of the
  535. * data, even if we've already copied over the requested data.
  536. *
  537. * This function adds the amount it transfers to *_offset, so this should be
  538. * precleared as appropriate. Note that the amount remaining in the buffer is
  539. * taken to be size - *_offset.
  540. *
  541. * *_abort should also be initialised to 0.
  542. */
  543. int rxrpc_kernel_recv_data(struct socket *sock, struct rxrpc_call *call,
  544. void *buf, size_t size, size_t *_offset,
  545. bool want_more, u32 *_abort)
  546. {
  547. struct iov_iter iter;
  548. struct kvec iov;
  549. int ret;
  550. _enter("{%d,%s},%zu/%zu,%d",
  551. call->debug_id, rxrpc_call_states[call->state],
  552. *_offset, size, want_more);
  553. ASSERTCMP(*_offset, <=, size);
  554. ASSERTCMP(call->state, !=, RXRPC_CALL_SERVER_ACCEPTING);
  555. iov.iov_base = buf + *_offset;
  556. iov.iov_len = size - *_offset;
  557. iov_iter_kvec(&iter, ITER_KVEC | READ, &iov, 1, size - *_offset);
  558. mutex_lock(&call->user_mutex);
  559. switch (READ_ONCE(call->state)) {
  560. case RXRPC_CALL_CLIENT_RECV_REPLY:
  561. case RXRPC_CALL_SERVER_RECV_REQUEST:
  562. case RXRPC_CALL_SERVER_ACK_REQUEST:
  563. ret = rxrpc_recvmsg_data(sock, call, NULL, &iter, size, 0,
  564. _offset);
  565. if (ret < 0)
  566. goto out;
  567. /* We can only reach here with a partially full buffer if we
  568. * have reached the end of the data. We must otherwise have a
  569. * full buffer or have been given -EAGAIN.
  570. */
  571. if (ret == 1) {
  572. if (*_offset < size)
  573. goto short_data;
  574. if (!want_more)
  575. goto read_phase_complete;
  576. ret = 0;
  577. goto out;
  578. }
  579. if (!want_more)
  580. goto excess_data;
  581. goto out;
  582. case RXRPC_CALL_COMPLETE:
  583. goto call_complete;
  584. default:
  585. ret = -EINPROGRESS;
  586. goto out;
  587. }
  588. read_phase_complete:
  589. ret = 1;
  590. out:
  591. mutex_unlock(&call->user_mutex);
  592. _leave(" = %d [%zu,%d]", ret, *_offset, *_abort);
  593. return ret;
  594. short_data:
  595. trace_rxrpc_rx_eproto(call, 0, tracepoint_string("short_data"));
  596. ret = -EBADMSG;
  597. goto out;
  598. excess_data:
  599. trace_rxrpc_rx_eproto(call, 0, tracepoint_string("excess_data"));
  600. ret = -EMSGSIZE;
  601. goto out;
  602. call_complete:
  603. *_abort = call->abort_code;
  604. ret = call->error;
  605. if (call->completion == RXRPC_CALL_SUCCEEDED) {
  606. ret = 1;
  607. if (size > 0)
  608. ret = -ECONNRESET;
  609. }
  610. goto out;
  611. }
  612. EXPORT_SYMBOL(rxrpc_kernel_recv_data);