ar-recvmsg.c 10 KB

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