ar-recvmsg.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  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. #include <linux/net.h>
  12. #include <linux/skbuff.h>
  13. #include <net/sock.h>
  14. #include <net/af_rxrpc.h>
  15. #include "ar-internal.h"
  16. /*
  17. * removal a call's user ID from the socket tree to make the user ID available
  18. * again and so that it won't be seen again in association with that call
  19. */
  20. void rxrpc_remove_user_ID(struct rxrpc_sock *rx, struct rxrpc_call *call)
  21. {
  22. _debug("RELEASE CALL %d", call->debug_id);
  23. if (test_bit(RXRPC_CALL_HAS_USERID, &call->flags)) {
  24. write_lock_bh(&rx->call_lock);
  25. rb_erase(&call->sock_node, &call->socket->calls);
  26. clear_bit(RXRPC_CALL_HAS_USERID, &call->flags);
  27. write_unlock_bh(&rx->call_lock);
  28. }
  29. read_lock_bh(&call->state_lock);
  30. if (!test_bit(RXRPC_CALL_RELEASED, &call->flags) &&
  31. !test_and_set_bit(RXRPC_CALL_RELEASE, &call->events))
  32. rxrpc_queue_call(call);
  33. read_unlock_bh(&call->state_lock);
  34. }
  35. /*
  36. * receive a message from an RxRPC socket
  37. * - we need to be careful about two or more threads calling recvmsg
  38. * simultaneously
  39. */
  40. int rxrpc_recvmsg(struct kiocb *iocb, struct socket *sock,
  41. struct msghdr *msg, size_t len, int flags)
  42. {
  43. struct rxrpc_skb_priv *sp;
  44. struct rxrpc_call *call = NULL, *continue_call = NULL;
  45. struct rxrpc_sock *rx = rxrpc_sk(sock->sk);
  46. struct sk_buff *skb;
  47. long timeo;
  48. int copy, ret, ullen, offset, copied = 0;
  49. u32 abort_code;
  50. DEFINE_WAIT(wait);
  51. _enter(",,,%zu,%d", len, flags);
  52. if (flags & (MSG_OOB | MSG_TRUNC))
  53. return -EOPNOTSUPP;
  54. ullen = msg->msg_flags & MSG_CMSG_COMPAT ? 4 : sizeof(unsigned long);
  55. timeo = sock_rcvtimeo(&rx->sk, flags & MSG_DONTWAIT);
  56. msg->msg_flags |= MSG_MORE;
  57. lock_sock(&rx->sk);
  58. for (;;) {
  59. /* return immediately if a client socket has no outstanding
  60. * calls */
  61. if (RB_EMPTY_ROOT(&rx->calls)) {
  62. if (copied)
  63. goto out;
  64. if (rx->sk.sk_state != RXRPC_SERVER_LISTENING) {
  65. release_sock(&rx->sk);
  66. if (continue_call)
  67. rxrpc_put_call(continue_call);
  68. return -ENODATA;
  69. }
  70. }
  71. /* get the next message on the Rx queue */
  72. skb = skb_peek(&rx->sk.sk_receive_queue);
  73. if (!skb) {
  74. /* nothing remains on the queue */
  75. if (copied &&
  76. (msg->msg_flags & MSG_PEEK || timeo == 0))
  77. goto out;
  78. /* wait for a message to turn up */
  79. release_sock(&rx->sk);
  80. prepare_to_wait_exclusive(rx->sk.sk_sleep, &wait,
  81. TASK_INTERRUPTIBLE);
  82. ret = sock_error(&rx->sk);
  83. if (ret)
  84. goto wait_error;
  85. if (skb_queue_empty(&rx->sk.sk_receive_queue)) {
  86. if (signal_pending(current))
  87. goto wait_interrupted;
  88. timeo = schedule_timeout(timeo);
  89. }
  90. finish_wait(rx->sk.sk_sleep, &wait);
  91. lock_sock(&rx->sk);
  92. continue;
  93. }
  94. peek_next_packet:
  95. sp = rxrpc_skb(skb);
  96. call = sp->call;
  97. ASSERT(call != NULL);
  98. _debug("next pkt %s", rxrpc_pkts[sp->hdr.type]);
  99. /* make sure we wait for the state to be updated in this call */
  100. spin_lock_bh(&call->lock);
  101. spin_unlock_bh(&call->lock);
  102. if (test_bit(RXRPC_CALL_RELEASED, &call->flags)) {
  103. _debug("packet from released call");
  104. if (skb_dequeue(&rx->sk.sk_receive_queue) != skb)
  105. BUG();
  106. rxrpc_free_skb(skb);
  107. continue;
  108. }
  109. /* determine whether to continue last data receive */
  110. if (continue_call) {
  111. _debug("maybe cont");
  112. if (call != continue_call ||
  113. skb->mark != RXRPC_SKB_MARK_DATA) {
  114. release_sock(&rx->sk);
  115. rxrpc_put_call(continue_call);
  116. _leave(" = %d [noncont]", copied);
  117. return copied;
  118. }
  119. }
  120. rxrpc_get_call(call);
  121. /* copy the peer address and timestamp */
  122. if (!continue_call) {
  123. if (msg->msg_name && msg->msg_namelen > 0)
  124. memcpy(&msg->msg_name, &call->conn->trans->peer->srx,
  125. sizeof(call->conn->trans->peer->srx));
  126. sock_recv_timestamp(msg, &rx->sk, skb);
  127. }
  128. /* receive the message */
  129. if (skb->mark != RXRPC_SKB_MARK_DATA)
  130. goto receive_non_data_message;
  131. _debug("recvmsg DATA #%u { %d, %d }",
  132. ntohl(sp->hdr.seq), skb->len, sp->offset);
  133. if (!continue_call) {
  134. /* only set the control data once per recvmsg() */
  135. ret = put_cmsg(msg, SOL_RXRPC, RXRPC_USER_CALL_ID,
  136. ullen, &call->user_call_ID);
  137. if (ret < 0)
  138. goto copy_error;
  139. ASSERT(test_bit(RXRPC_CALL_HAS_USERID, &call->flags));
  140. }
  141. ASSERTCMP(ntohl(sp->hdr.seq), >=, call->rx_data_recv);
  142. ASSERTCMP(ntohl(sp->hdr.seq), <=, call->rx_data_recv + 1);
  143. call->rx_data_recv = ntohl(sp->hdr.seq);
  144. ASSERTCMP(ntohl(sp->hdr.seq), >, call->rx_data_eaten);
  145. offset = sp->offset;
  146. copy = skb->len - offset;
  147. if (copy > len - copied)
  148. copy = len - copied;
  149. if (skb->ip_summed == CHECKSUM_UNNECESSARY) {
  150. ret = skb_copy_datagram_iovec(skb, offset,
  151. msg->msg_iov, copy);
  152. } else {
  153. ret = skb_copy_and_csum_datagram_iovec(skb, offset,
  154. msg->msg_iov);
  155. if (ret == -EINVAL)
  156. goto csum_copy_error;
  157. }
  158. if (ret < 0)
  159. goto copy_error;
  160. /* handle piecemeal consumption of data packets */
  161. _debug("copied %d+%d", copy, copied);
  162. offset += copy;
  163. copied += copy;
  164. if (!(flags & MSG_PEEK))
  165. sp->offset = offset;
  166. if (sp->offset < skb->len) {
  167. _debug("buffer full");
  168. ASSERTCMP(copied, ==, len);
  169. break;
  170. }
  171. /* we transferred the whole data packet */
  172. if (sp->hdr.flags & RXRPC_LAST_PACKET) {
  173. _debug("last");
  174. if (call->conn->out_clientflag) {
  175. /* last byte of reply received */
  176. ret = copied;
  177. goto terminal_message;
  178. }
  179. /* last bit of request received */
  180. if (!(flags & MSG_PEEK)) {
  181. _debug("eat packet");
  182. if (skb_dequeue(&rx->sk.sk_receive_queue) !=
  183. skb)
  184. BUG();
  185. rxrpc_free_skb(skb);
  186. }
  187. msg->msg_flags &= ~MSG_MORE;
  188. break;
  189. }
  190. /* move on to the next data message */
  191. _debug("next");
  192. if (!continue_call)
  193. continue_call = sp->call;
  194. else
  195. rxrpc_put_call(call);
  196. call = NULL;
  197. if (flags & MSG_PEEK) {
  198. _debug("peek next");
  199. skb = skb->next;
  200. if (skb == (struct sk_buff *) &rx->sk.sk_receive_queue)
  201. break;
  202. goto peek_next_packet;
  203. }
  204. _debug("eat packet");
  205. if (skb_dequeue(&rx->sk.sk_receive_queue) != skb)
  206. BUG();
  207. rxrpc_free_skb(skb);
  208. }
  209. /* end of non-terminal data packet reception for the moment */
  210. _debug("end rcv data");
  211. out:
  212. release_sock(&rx->sk);
  213. if (call)
  214. rxrpc_put_call(call);
  215. if (continue_call)
  216. rxrpc_put_call(continue_call);
  217. _leave(" = %d [data]", copied);
  218. return copied;
  219. /* handle non-DATA messages such as aborts, incoming connections and
  220. * final ACKs */
  221. receive_non_data_message:
  222. _debug("non-data");
  223. if (skb->mark == RXRPC_SKB_MARK_NEW_CALL) {
  224. _debug("RECV NEW CALL");
  225. ret = put_cmsg(msg, SOL_RXRPC, RXRPC_NEW_CALL, 0, &abort_code);
  226. if (ret < 0)
  227. goto copy_error;
  228. if (!(flags & MSG_PEEK)) {
  229. if (skb_dequeue(&rx->sk.sk_receive_queue) != skb)
  230. BUG();
  231. rxrpc_free_skb(skb);
  232. }
  233. goto out;
  234. }
  235. ret = put_cmsg(msg, SOL_RXRPC, RXRPC_USER_CALL_ID,
  236. ullen, &call->user_call_ID);
  237. if (ret < 0)
  238. goto copy_error;
  239. ASSERT(test_bit(RXRPC_CALL_HAS_USERID, &call->flags));
  240. switch (skb->mark) {
  241. case RXRPC_SKB_MARK_DATA:
  242. BUG();
  243. case RXRPC_SKB_MARK_FINAL_ACK:
  244. ret = put_cmsg(msg, SOL_RXRPC, RXRPC_ACK, 0, &abort_code);
  245. break;
  246. case RXRPC_SKB_MARK_BUSY:
  247. ret = put_cmsg(msg, SOL_RXRPC, RXRPC_BUSY, 0, &abort_code);
  248. break;
  249. case RXRPC_SKB_MARK_REMOTE_ABORT:
  250. abort_code = call->abort_code;
  251. ret = put_cmsg(msg, SOL_RXRPC, RXRPC_ABORT, 4, &abort_code);
  252. break;
  253. case RXRPC_SKB_MARK_NET_ERROR:
  254. _debug("RECV NET ERROR %d", sp->error);
  255. abort_code = sp->error;
  256. ret = put_cmsg(msg, SOL_RXRPC, RXRPC_NET_ERROR, 4, &abort_code);
  257. break;
  258. case RXRPC_SKB_MARK_LOCAL_ERROR:
  259. _debug("RECV LOCAL ERROR %d", sp->error);
  260. abort_code = sp->error;
  261. ret = put_cmsg(msg, SOL_RXRPC, RXRPC_LOCAL_ERROR, 4,
  262. &abort_code);
  263. break;
  264. default:
  265. BUG();
  266. break;
  267. }
  268. if (ret < 0)
  269. goto copy_error;
  270. terminal_message:
  271. _debug("terminal");
  272. msg->msg_flags &= ~MSG_MORE;
  273. msg->msg_flags |= MSG_EOR;
  274. if (!(flags & MSG_PEEK)) {
  275. _net("free terminal skb %p", skb);
  276. if (skb_dequeue(&rx->sk.sk_receive_queue) != skb)
  277. BUG();
  278. rxrpc_free_skb(skb);
  279. rxrpc_remove_user_ID(rx, call);
  280. }
  281. release_sock(&rx->sk);
  282. rxrpc_put_call(call);
  283. if (continue_call)
  284. rxrpc_put_call(continue_call);
  285. _leave(" = %d", ret);
  286. return ret;
  287. copy_error:
  288. _debug("copy error");
  289. release_sock(&rx->sk);
  290. rxrpc_put_call(call);
  291. if (continue_call)
  292. rxrpc_put_call(continue_call);
  293. _leave(" = %d", ret);
  294. return ret;
  295. csum_copy_error:
  296. _debug("csum error");
  297. release_sock(&rx->sk);
  298. if (continue_call)
  299. rxrpc_put_call(continue_call);
  300. rxrpc_kill_skb(skb);
  301. skb_kill_datagram(&rx->sk, skb, flags);
  302. rxrpc_put_call(call);
  303. return -EAGAIN;
  304. wait_interrupted:
  305. ret = sock_intr_errno(timeo);
  306. wait_error:
  307. finish_wait(rx->sk.sk_sleep, &wait);
  308. if (continue_call)
  309. rxrpc_put_call(continue_call);
  310. if (copied)
  311. copied = ret;
  312. _leave(" = %d [waitfail %d]", copied, ret);
  313. return copied;
  314. }
  315. /**
  316. * rxrpc_kernel_data_delivered - Record delivery of data message
  317. * @skb: Message holding data
  318. *
  319. * Record the delivery of a data message. This permits RxRPC to keep its
  320. * tracking correct. The socket buffer will be deleted.
  321. */
  322. void rxrpc_kernel_data_delivered(struct sk_buff *skb)
  323. {
  324. struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
  325. struct rxrpc_call *call = sp->call;
  326. ASSERTCMP(ntohl(sp->hdr.seq), >=, call->rx_data_recv);
  327. ASSERTCMP(ntohl(sp->hdr.seq), <=, call->rx_data_recv + 1);
  328. call->rx_data_recv = ntohl(sp->hdr.seq);
  329. ASSERTCMP(ntohl(sp->hdr.seq), >, call->rx_data_eaten);
  330. rxrpc_free_skb(skb);
  331. }
  332. EXPORT_SYMBOL(rxrpc_kernel_data_delivered);
  333. /**
  334. * rxrpc_kernel_is_data_last - Determine if data message is last one
  335. * @skb: Message holding data
  336. *
  337. * Determine if data message is last one for the parent call.
  338. */
  339. bool rxrpc_kernel_is_data_last(struct sk_buff *skb)
  340. {
  341. struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
  342. ASSERTCMP(skb->mark, ==, RXRPC_SKB_MARK_DATA);
  343. return sp->hdr.flags & RXRPC_LAST_PACKET;
  344. }
  345. EXPORT_SYMBOL(rxrpc_kernel_is_data_last);
  346. /**
  347. * rxrpc_kernel_get_abort_code - Get the abort code from an RxRPC abort message
  348. * @skb: Message indicating an abort
  349. *
  350. * Get the abort code from an RxRPC abort message.
  351. */
  352. u32 rxrpc_kernel_get_abort_code(struct sk_buff *skb)
  353. {
  354. struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
  355. ASSERTCMP(skb->mark, ==, RXRPC_SKB_MARK_REMOTE_ABORT);
  356. return sp->call->abort_code;
  357. }
  358. EXPORT_SYMBOL(rxrpc_kernel_get_abort_code);
  359. /**
  360. * rxrpc_kernel_get_error - Get the error number from an RxRPC error message
  361. * @skb: Message indicating an error
  362. *
  363. * Get the error number from an RxRPC error message.
  364. */
  365. int rxrpc_kernel_get_error_number(struct sk_buff *skb)
  366. {
  367. struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
  368. return sp->error;
  369. }
  370. EXPORT_SYMBOL(rxrpc_kernel_get_error_number);