PageRenderTime 47ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

/net/rxrpc/ar-recvmsg.c

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