recvmsg.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  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. * removal a call's user ID from the socket tree to make the user ID available
  20. * again and so that it won't be seen again in association with that call
  21. */
  22. void rxrpc_remove_user_ID(struct rxrpc_sock *rx, struct rxrpc_call *call)
  23. {
  24. _debug("RELEASE CALL %d", call->debug_id);
  25. if (test_bit(RXRPC_CALL_HAS_USERID, &call->flags)) {
  26. write_lock_bh(&rx->call_lock);
  27. rb_erase(&call->sock_node, &call->socket->calls);
  28. clear_bit(RXRPC_CALL_HAS_USERID, &call->flags);
  29. write_unlock_bh(&rx->call_lock);
  30. }
  31. read_lock_bh(&call->state_lock);
  32. if (!test_bit(RXRPC_CALL_RELEASED, &call->flags) &&
  33. !test_and_set_bit(RXRPC_CALL_EV_RELEASE, &call->events))
  34. rxrpc_queue_call(call);
  35. read_unlock_bh(&call->state_lock);
  36. }
  37. /*
  38. * receive a message from an RxRPC socket
  39. * - we need to be careful about two or more threads calling recvmsg
  40. * simultaneously
  41. */
  42. int rxrpc_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
  43. int flags)
  44. {
  45. struct rxrpc_skb_priv *sp;
  46. struct rxrpc_call *call = NULL, *continue_call = NULL;
  47. struct rxrpc_sock *rx = rxrpc_sk(sock->sk);
  48. struct sk_buff *skb;
  49. long timeo;
  50. int copy, ret, ullen, offset, copied = 0;
  51. u32 abort_code;
  52. DEFINE_WAIT(wait);
  53. _enter(",,,%zu,%d", len, flags);
  54. if (flags & (MSG_OOB | MSG_TRUNC))
  55. return -EOPNOTSUPP;
  56. ullen = msg->msg_flags & MSG_CMSG_COMPAT ? 4 : sizeof(unsigned long);
  57. timeo = sock_rcvtimeo(&rx->sk, flags & MSG_DONTWAIT);
  58. msg->msg_flags |= MSG_MORE;
  59. lock_sock(&rx->sk);
  60. for (;;) {
  61. /* return immediately if a client socket has no outstanding
  62. * calls */
  63. if (RB_EMPTY_ROOT(&rx->calls)) {
  64. if (copied)
  65. goto out;
  66. if (rx->sk.sk_state != RXRPC_SERVER_LISTENING) {
  67. release_sock(&rx->sk);
  68. if (continue_call)
  69. rxrpc_put_call(continue_call);
  70. return -ENODATA;
  71. }
  72. }
  73. /* get the next message on the Rx queue */
  74. skb = skb_peek(&rx->sk.sk_receive_queue);
  75. if (!skb) {
  76. /* nothing remains on the queue */
  77. if (copied &&
  78. (flags & MSG_PEEK || timeo == 0))
  79. goto out;
  80. /* wait for a message to turn up */
  81. release_sock(&rx->sk);
  82. prepare_to_wait_exclusive(sk_sleep(&rx->sk), &wait,
  83. TASK_INTERRUPTIBLE);
  84. ret = sock_error(&rx->sk);
  85. if (ret)
  86. goto wait_error;
  87. if (skb_queue_empty(&rx->sk.sk_receive_queue)) {
  88. if (signal_pending(current))
  89. goto wait_interrupted;
  90. timeo = schedule_timeout(timeo);
  91. }
  92. finish_wait(sk_sleep(&rx->sk), &wait);
  93. lock_sock(&rx->sk);
  94. continue;
  95. }
  96. peek_next_packet:
  97. rxrpc_see_skb(skb);
  98. sp = rxrpc_skb(skb);
  99. call = sp->call;
  100. ASSERT(call != NULL);
  101. _debug("next pkt %s", rxrpc_pkts[sp->hdr.type]);
  102. /* make sure we wait for the state to be updated in this call */
  103. spin_lock_bh(&call->lock);
  104. spin_unlock_bh(&call->lock);
  105. if (test_bit(RXRPC_CALL_RELEASED, &call->flags)) {
  106. _debug("packet from released call");
  107. if (skb_dequeue(&rx->sk.sk_receive_queue) != skb)
  108. BUG();
  109. rxrpc_free_skb(skb);
  110. continue;
  111. }
  112. /* determine whether to continue last data receive */
  113. if (continue_call) {
  114. _debug("maybe cont");
  115. if (call != continue_call ||
  116. skb->mark != RXRPC_SKB_MARK_DATA) {
  117. release_sock(&rx->sk);
  118. rxrpc_put_call(continue_call);
  119. _leave(" = %d [noncont]", copied);
  120. return copied;
  121. }
  122. }
  123. rxrpc_get_call(call);
  124. /* copy the peer address and timestamp */
  125. if (!continue_call) {
  126. if (msg->msg_name) {
  127. size_t len =
  128. sizeof(call->conn->params.peer->srx);
  129. memcpy(msg->msg_name,
  130. &call->conn->params.peer->srx, len);
  131. msg->msg_namelen = len;
  132. }
  133. sock_recv_timestamp(msg, &rx->sk, skb);
  134. }
  135. /* receive the message */
  136. if (skb->mark != RXRPC_SKB_MARK_DATA)
  137. goto receive_non_data_message;
  138. _debug("recvmsg DATA #%u { %d, %d }",
  139. sp->hdr.seq, skb->len, sp->offset);
  140. if (!continue_call) {
  141. /* only set the control data once per recvmsg() */
  142. ret = put_cmsg(msg, SOL_RXRPC, RXRPC_USER_CALL_ID,
  143. ullen, &call->user_call_ID);
  144. if (ret < 0)
  145. goto copy_error;
  146. ASSERT(test_bit(RXRPC_CALL_HAS_USERID, &call->flags));
  147. }
  148. ASSERTCMP(sp->hdr.seq, >=, call->rx_data_recv);
  149. ASSERTCMP(sp->hdr.seq, <=, call->rx_data_recv + 1);
  150. call->rx_data_recv = sp->hdr.seq;
  151. ASSERTCMP(sp->hdr.seq, >, call->rx_data_eaten);
  152. offset = sp->offset;
  153. copy = skb->len - offset;
  154. if (copy > len - copied)
  155. copy = len - copied;
  156. ret = skb_copy_datagram_msg(skb, offset, msg, copy);
  157. if (ret < 0)
  158. goto copy_error;
  159. /* handle piecemeal consumption of data packets */
  160. _debug("copied %d+%d", copy, copied);
  161. offset += copy;
  162. copied += copy;
  163. if (!(flags & MSG_PEEK))
  164. sp->offset = offset;
  165. if (sp->offset < skb->len) {
  166. _debug("buffer full");
  167. ASSERTCMP(copied, ==, len);
  168. break;
  169. }
  170. /* we transferred the whole data packet */
  171. if (!(flags & MSG_PEEK))
  172. rxrpc_kernel_data_consumed(call, skb);
  173. if (sp->hdr.flags & RXRPC_LAST_PACKET) {
  174. _debug("last");
  175. if (rxrpc_conn_is_client(call->conn)) {
  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->remote_abort;
  252. ret = put_cmsg(msg, SOL_RXRPC, RXRPC_ABORT, 4, &abort_code);
  253. break;
  254. case RXRPC_SKB_MARK_LOCAL_ABORT:
  255. abort_code = call->local_abort;
  256. ret = put_cmsg(msg, SOL_RXRPC, RXRPC_ABORT, 4, &abort_code);
  257. break;
  258. case RXRPC_SKB_MARK_NET_ERROR:
  259. _debug("RECV NET ERROR %d", sp->error);
  260. abort_code = sp->error;
  261. ret = put_cmsg(msg, SOL_RXRPC, RXRPC_NET_ERROR, 4, &abort_code);
  262. break;
  263. case RXRPC_SKB_MARK_LOCAL_ERROR:
  264. _debug("RECV LOCAL ERROR %d", sp->error);
  265. abort_code = sp->error;
  266. ret = put_cmsg(msg, SOL_RXRPC, RXRPC_LOCAL_ERROR, 4,
  267. &abort_code);
  268. break;
  269. default:
  270. pr_err("Unknown packet mark %u\n", skb->mark);
  271. BUG();
  272. break;
  273. }
  274. if (ret < 0)
  275. goto copy_error;
  276. terminal_message:
  277. _debug("terminal");
  278. msg->msg_flags &= ~MSG_MORE;
  279. msg->msg_flags |= MSG_EOR;
  280. if (!(flags & MSG_PEEK)) {
  281. _net("free terminal skb %p", skb);
  282. if (skb_dequeue(&rx->sk.sk_receive_queue) != skb)
  283. BUG();
  284. rxrpc_free_skb(skb);
  285. rxrpc_remove_user_ID(rx, call);
  286. }
  287. release_sock(&rx->sk);
  288. rxrpc_put_call(call);
  289. if (continue_call)
  290. rxrpc_put_call(continue_call);
  291. _leave(" = %d", ret);
  292. return ret;
  293. copy_error:
  294. _debug("copy error");
  295. release_sock(&rx->sk);
  296. rxrpc_put_call(call);
  297. if (continue_call)
  298. rxrpc_put_call(continue_call);
  299. _leave(" = %d", ret);
  300. return ret;
  301. wait_interrupted:
  302. ret = sock_intr_errno(timeo);
  303. wait_error:
  304. finish_wait(sk_sleep(&rx->sk), &wait);
  305. if (continue_call)
  306. rxrpc_put_call(continue_call);
  307. if (copied)
  308. copied = ret;
  309. _leave(" = %d [waitfail %d]", copied, ret);
  310. return copied;
  311. }
  312. /**
  313. * rxrpc_kernel_is_data_last - Determine if data message is last one
  314. * @skb: Message holding data
  315. *
  316. * Determine if data message is last one for the parent call.
  317. */
  318. bool rxrpc_kernel_is_data_last(struct sk_buff *skb)
  319. {
  320. struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
  321. ASSERTCMP(skb->mark, ==, RXRPC_SKB_MARK_DATA);
  322. return sp->hdr.flags & RXRPC_LAST_PACKET;
  323. }
  324. EXPORT_SYMBOL(rxrpc_kernel_is_data_last);
  325. /**
  326. * rxrpc_kernel_get_abort_code - Get the abort code from an RxRPC abort message
  327. * @skb: Message indicating an abort
  328. *
  329. * Get the abort code from an RxRPC abort message.
  330. */
  331. u32 rxrpc_kernel_get_abort_code(struct sk_buff *skb)
  332. {
  333. struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
  334. switch (skb->mark) {
  335. case RXRPC_SKB_MARK_REMOTE_ABORT:
  336. return sp->call->remote_abort;
  337. case RXRPC_SKB_MARK_LOCAL_ABORT:
  338. return sp->call->local_abort;
  339. default:
  340. BUG();
  341. }
  342. }
  343. EXPORT_SYMBOL(rxrpc_kernel_get_abort_code);
  344. /**
  345. * rxrpc_kernel_get_error - Get the error number from an RxRPC error message
  346. * @skb: Message indicating an error
  347. *
  348. * Get the error number from an RxRPC error message.
  349. */
  350. int rxrpc_kernel_get_error_number(struct sk_buff *skb)
  351. {
  352. struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
  353. return sp->error;
  354. }
  355. EXPORT_SYMBOL(rxrpc_kernel_get_error_number);