recvmsg.c 17 KB

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