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 <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(sk_sleep(&rx->sk), &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(sk_sleep(&rx->sk), &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,
  125. &call->conn->trans->peer->srx,
  126. sizeof(call->conn->trans->peer->srx));
  127. sock_recv_ts_and_drops(msg, &rx->sk, skb);
  128. }
  129. /* receive the message */
  130. if (skb->mark != RXRPC_SKB_MARK_DATA)
  131. goto receive_non_data_message;
  132. _debug("recvmsg DATA #%u { %d, %d }",
  133. ntohl(sp->hdr.seq), skb->len, sp->offset);
  134. if (!continue_call) {
  135. /* only set the control data once per recvmsg() */
  136. ret = put_cmsg(msg, SOL_RXRPC, RXRPC_USER_CALL_ID,
  137. ullen, &call->user_call_ID);
  138. if (ret < 0)
  139. goto copy_error;
  140. ASSERT(test_bit(RXRPC_CALL_HAS_USERID, &call->flags));
  141. }
  142. ASSERTCMP(ntohl(sp->hdr.seq), >=, call->rx_data_recv);
  143. ASSERTCMP(ntohl(sp->hdr.seq), <=, call->rx_data_recv + 1);
  144. call->rx_data_recv = ntohl(sp->hdr.seq);
  145. ASSERTCMP(ntohl(sp->hdr.seq), >, call->rx_data_eaten);
  146. offset = sp->offset;
  147. copy = skb->len - offset;
  148. if (copy > len - copied)
  149. copy = len - copied;
  150. if (skb->ip_summed == CHECKSUM_UNNECESSARY) {
  151. ret = skb_copy_datagram_iovec(skb, offset,
  152. msg->msg_iov, copy);
  153. } else {
  154. ret = skb_copy_and_csum_datagram_iovec(skb, offset,
  155. msg->msg_iov);
  156. if (ret == -EINVAL)
  157. goto csum_copy_error;
  158. }
  159. if (ret < 0)
  160. goto copy_error;
  161. /* handle piecemeal consumption of data packets */
  162. _debug("copied %d+%d", copy, copied);
  163. offset += copy;
  164. copied += copy;
  165. if (!(flags & MSG_PEEK))
  166. sp->offset = offset;
  167. if (sp->offset < skb->len) {
  168. _debug("buffer full");
  169. ASSERTCMP(copied, ==, len);
  170. break;
  171. }
  172. /* we transferred the whole data packet */
  173. if (sp->hdr.flags & RXRPC_LAST_PACKET) {
  174. _debug("last");
  175. if (call->conn->out_clientflag) {
  176. /* last byte of reply received */
  177. ret = copied;
  178. goto terminal_message;
  179. }
  180. /* last bit of request received */
  181. if (!(flags & MSG_PEEK)) {
  182. _debug("eat packet");
  183. if (skb_dequeue(&rx->sk.sk_receive_queue) !=
  184. skb)
  185. BUG();
  186. rxrpc_free_skb(skb);
  187. }
  188. msg->msg_flags &= ~MSG_MORE;
  189. break;
  190. }
  191. /* move on to the next data message */
  192. _debug("next");
  193. if (!continue_call)
  194. continue_call = sp->call;
  195. else
  196. rxrpc_put_call(call);
  197. call = NULL;
  198. if (flags & MSG_PEEK) {
  199. _debug("peek next");
  200. skb = skb->next;
  201. if (skb == (struct sk_buff *) &rx->sk.sk_receive_queue)
  202. break;
  203. goto peek_next_packet;
  204. }
  205. _debug("eat packet");
  206. if (skb_dequeue(&rx->sk.sk_receive_queue) != skb)
  207. BUG();
  208. rxrpc_free_skb(skb);
  209. }
  210. /* end of non-terminal data packet reception for the moment */
  211. _debug("end rcv data");
  212. out:
  213. release_sock(&rx->sk);
  214. if (call)
  215. rxrpc_put_call(call);
  216. if (continue_call)
  217. rxrpc_put_call(continue_call);
  218. _leave(" = %d [data]", copied);
  219. return copied;
  220. /* handle non-DATA messages such as aborts, incoming connections and
  221. * final ACKs */
  222. receive_non_data_message:
  223. _debug("non-data");
  224. if (skb->mark == RXRPC_SKB_MARK_NEW_CALL) {
  225. _debug("RECV NEW CALL");
  226. ret = put_cmsg(msg, SOL_RXRPC, RXRPC_NEW_CALL, 0, &abort_code);
  227. if (ret < 0)
  228. goto copy_error;
  229. if (!(flags & MSG_PEEK)) {
  230. if (skb_dequeue(&rx->sk.sk_receive_queue) != skb)
  231. BUG();
  232. rxrpc_free_skb(skb);
  233. }
  234. goto out;
  235. }
  236. ret = put_cmsg(msg, SOL_RXRPC, RXRPC_USER_CALL_ID,
  237. ullen, &call->user_call_ID);
  238. if (ret < 0)
  239. goto copy_error;
  240. ASSERT(test_bit(RXRPC_CALL_HAS_USERID, &call->flags));
  241. switch (skb->mark) {
  242. case RXRPC_SKB_MARK_DATA:
  243. BUG();
  244. case RXRPC_SKB_MARK_FINAL_ACK:
  245. ret = put_cmsg(msg, SOL_RXRPC, RXRPC_ACK, 0, &abort_code);
  246. break;
  247. case RXRPC_SKB_MARK_BUSY:
  248. ret = put_cmsg(msg, SOL_RXRPC, RXRPC_BUSY, 0, &abort_code);
  249. break;
  250. case RXRPC_SKB_MARK_REMOTE_ABORT:
  251. abort_code = call->abort_code;
  252. ret = put_cmsg(msg, SOL_RXRPC, RXRPC_ABORT, 4, &abort_code);
  253. break;
  254. case RXRPC_SKB_MARK_NET_ERROR:
  255. _debug("RECV NET ERROR %d", sp->error);
  256. abort_code = sp->error;
  257. ret = put_cmsg(msg, SOL_RXRPC, RXRPC_NET_ERROR, 4, &abort_code);
  258. break;
  259. case RXRPC_SKB_MARK_LOCAL_ERROR:
  260. _debug("RECV LOCAL ERROR %d", sp->error);
  261. abort_code = sp->error;
  262. ret = put_cmsg(msg, SOL_RXRPC, RXRPC_LOCAL_ERROR, 4,
  263. &abort_code);
  264. break;
  265. default:
  266. BUG();
  267. break;
  268. }
  269. if (ret < 0)
  270. goto copy_error;
  271. terminal_message:
  272. _debug("terminal");
  273. msg->msg_flags &= ~MSG_MORE;
  274. msg->msg_flags |= MSG_EOR;
  275. if (!(flags & MSG_PEEK)) {
  276. _net("free terminal skb %p", skb);
  277. if (skb_dequeue(&rx->sk.sk_receive_queue) != skb)
  278. BUG();
  279. rxrpc_free_skb(skb);
  280. rxrpc_remove_user_ID(rx, call);
  281. }
  282. release_sock(&rx->sk);
  283. rxrpc_put_call(call);
  284. if (continue_call)
  285. rxrpc_put_call(continue_call);
  286. _leave(" = %d", ret);
  287. return ret;
  288. copy_error:
  289. _debug("copy error");
  290. release_sock(&rx->sk);
  291. rxrpc_put_call(call);
  292. if (continue_call)
  293. rxrpc_put_call(continue_call);
  294. _leave(" = %d", ret);
  295. return ret;
  296. csum_copy_error:
  297. _debug("csum error");
  298. release_sock(&rx->sk);
  299. if (continue_call)
  300. rxrpc_put_call(continue_call);
  301. rxrpc_kill_skb(skb);
  302. skb_kill_datagram(&rx->sk, skb, flags);
  303. rxrpc_put_call(call);
  304. return -EAGAIN;
  305. wait_interrupted:
  306. ret = sock_intr_errno(timeo);
  307. wait_error:
  308. finish_wait(sk_sleep(&rx->sk), &wait);
  309. if (continue_call)
  310. rxrpc_put_call(continue_call);
  311. if (copied)
  312. copied = ret;
  313. _leave(" = %d [waitfail %d]", copied, ret);
  314. return copied;
  315. }
  316. /**
  317. * rxrpc_kernel_data_delivered - Record delivery of data message
  318. * @skb: Message holding data
  319. *
  320. * Record the delivery of a data message. This permits RxRPC to keep its
  321. * tracking correct. The socket buffer will be deleted.
  322. */
  323. void rxrpc_kernel_data_delivered(struct sk_buff *skb)
  324. {
  325. struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
  326. struct rxrpc_call *call = sp->call;
  327. ASSERTCMP(ntohl(sp->hdr.seq), >=, call->rx_data_recv);
  328. ASSERTCMP(ntohl(sp->hdr.seq), <=, call->rx_data_recv + 1);
  329. call->rx_data_recv = ntohl(sp->hdr.seq);
  330. ASSERTCMP(ntohl(sp->hdr.seq), >, call->rx_data_eaten);
  331. rxrpc_free_skb(skb);
  332. }
  333. EXPORT_SYMBOL(rxrpc_kernel_data_delivered);
  334. /**
  335. * rxrpc_kernel_is_data_last - Determine if data message is last one
  336. * @skb: Message holding data
  337. *
  338. * Determine if data message is last one for the parent call.
  339. */
  340. bool rxrpc_kernel_is_data_last(struct sk_buff *skb)
  341. {
  342. struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
  343. ASSERTCMP(skb->mark, ==, RXRPC_SKB_MARK_DATA);
  344. return sp->hdr.flags & RXRPC_LAST_PACKET;
  345. }
  346. EXPORT_SYMBOL(rxrpc_kernel_is_data_last);
  347. /**
  348. * rxrpc_kernel_get_abort_code - Get the abort code from an RxRPC abort message
  349. * @skb: Message indicating an abort
  350. *
  351. * Get the abort code from an RxRPC abort message.
  352. */
  353. u32 rxrpc_kernel_get_abort_code(struct sk_buff *skb)
  354. {
  355. struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
  356. ASSERTCMP(skb->mark, ==, RXRPC_SKB_MARK_REMOTE_ABORT);
  357. return sp->call->abort_code;
  358. }
  359. EXPORT_SYMBOL(rxrpc_kernel_get_abort_code);
  360. /**
  361. * rxrpc_kernel_get_error - Get the error number from an RxRPC error message
  362. * @skb: Message indicating an error
  363. *
  364. * Get the error number from an RxRPC error message.
  365. */
  366. int rxrpc_kernel_get_error_number(struct sk_buff *skb)
  367. {
  368. struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
  369. return sp->error;
  370. }
  371. EXPORT_SYMBOL(rxrpc_kernel_get_error_number);