ar-recvmsg.c 11 KB

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