sendmsg.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748
  1. /* AF_RXRPC sendmsg() implementation.
  2. *
  3. * Copyright (C) 2007, 2016 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 Licence
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the Licence, or (at your option) any later version.
  10. */
  11. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  12. #include <linux/net.h>
  13. #include <linux/gfp.h>
  14. #include <linux/skbuff.h>
  15. #include <linux/export.h>
  16. #include <linux/sched/signal.h>
  17. #include <net/sock.h>
  18. #include <net/af_rxrpc.h>
  19. #include "ar-internal.h"
  20. enum rxrpc_command {
  21. RXRPC_CMD_SEND_DATA, /* send data message */
  22. RXRPC_CMD_SEND_ABORT, /* request abort generation */
  23. RXRPC_CMD_ACCEPT, /* [server] accept incoming call */
  24. RXRPC_CMD_REJECT_BUSY, /* [server] reject a call as busy */
  25. };
  26. struct rxrpc_send_params {
  27. s64 tx_total_len; /* Total Tx data length (if send data) */
  28. unsigned long user_call_ID; /* User's call ID */
  29. u32 abort_code; /* Abort code to Tx (if abort) */
  30. enum rxrpc_command command : 8; /* The command to implement */
  31. bool exclusive; /* Shared or exclusive call */
  32. bool upgrade; /* If the connection is upgradeable */
  33. };
  34. /*
  35. * wait for space to appear in the transmit/ACK window
  36. * - caller holds the socket locked
  37. */
  38. static int rxrpc_wait_for_tx_window(struct rxrpc_sock *rx,
  39. struct rxrpc_call *call,
  40. long *timeo)
  41. {
  42. DECLARE_WAITQUEUE(myself, current);
  43. int ret;
  44. _enter(",{%u,%u,%u}",
  45. call->tx_hard_ack, call->tx_top, call->tx_winsize);
  46. add_wait_queue(&call->waitq, &myself);
  47. for (;;) {
  48. set_current_state(TASK_INTERRUPTIBLE);
  49. ret = 0;
  50. if (call->tx_top - call->tx_hard_ack <
  51. min_t(unsigned int, call->tx_winsize,
  52. call->cong_cwnd + call->cong_extra))
  53. break;
  54. if (call->state >= RXRPC_CALL_COMPLETE) {
  55. ret = call->error;
  56. break;
  57. }
  58. if (signal_pending(current)) {
  59. ret = sock_intr_errno(*timeo);
  60. break;
  61. }
  62. trace_rxrpc_transmit(call, rxrpc_transmit_wait);
  63. mutex_unlock(&call->user_mutex);
  64. *timeo = schedule_timeout(*timeo);
  65. if (mutex_lock_interruptible(&call->user_mutex) < 0) {
  66. ret = sock_intr_errno(*timeo);
  67. break;
  68. }
  69. }
  70. remove_wait_queue(&call->waitq, &myself);
  71. set_current_state(TASK_RUNNING);
  72. _leave(" = %d", ret);
  73. return ret;
  74. }
  75. /*
  76. * Schedule an instant Tx resend.
  77. */
  78. static inline void rxrpc_instant_resend(struct rxrpc_call *call, int ix)
  79. {
  80. spin_lock_bh(&call->lock);
  81. if (call->state < RXRPC_CALL_COMPLETE) {
  82. call->rxtx_annotations[ix] = RXRPC_TX_ANNO_RETRANS;
  83. if (!test_and_set_bit(RXRPC_CALL_EV_RESEND, &call->events))
  84. rxrpc_queue_call(call);
  85. }
  86. spin_unlock_bh(&call->lock);
  87. }
  88. /*
  89. * Notify the owner of the call that the transmit phase is ended and the last
  90. * packet has been queued.
  91. */
  92. static void rxrpc_notify_end_tx(struct rxrpc_sock *rx, struct rxrpc_call *call,
  93. rxrpc_notify_end_tx_t notify_end_tx)
  94. {
  95. if (notify_end_tx)
  96. notify_end_tx(&rx->sk, call, call->user_call_ID);
  97. }
  98. /*
  99. * Queue a DATA packet for transmission, set the resend timeout and send the
  100. * packet immediately
  101. */
  102. static void rxrpc_queue_packet(struct rxrpc_sock *rx, struct rxrpc_call *call,
  103. struct sk_buff *skb, bool last,
  104. rxrpc_notify_end_tx_t notify_end_tx)
  105. {
  106. struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
  107. rxrpc_seq_t seq = sp->hdr.seq;
  108. int ret, ix;
  109. u8 annotation = RXRPC_TX_ANNO_UNACK;
  110. _net("queue skb %p [%d]", skb, seq);
  111. ASSERTCMP(seq, ==, call->tx_top + 1);
  112. if (last) {
  113. annotation |= RXRPC_TX_ANNO_LAST;
  114. set_bit(RXRPC_CALL_TX_LASTQ, &call->flags);
  115. }
  116. /* We have to set the timestamp before queueing as the retransmit
  117. * algorithm can see the packet as soon as we queue it.
  118. */
  119. skb->tstamp = ktime_get_real();
  120. ix = seq & RXRPC_RXTX_BUFF_MASK;
  121. rxrpc_get_skb(skb, rxrpc_skb_tx_got);
  122. call->rxtx_annotations[ix] = annotation;
  123. smp_wmb();
  124. call->rxtx_buffer[ix] = skb;
  125. call->tx_top = seq;
  126. if (last)
  127. trace_rxrpc_transmit(call, rxrpc_transmit_queue_last);
  128. else
  129. trace_rxrpc_transmit(call, rxrpc_transmit_queue);
  130. if (last || call->state == RXRPC_CALL_SERVER_ACK_REQUEST) {
  131. _debug("________awaiting reply/ACK__________");
  132. write_lock_bh(&call->state_lock);
  133. switch (call->state) {
  134. case RXRPC_CALL_CLIENT_SEND_REQUEST:
  135. call->state = RXRPC_CALL_CLIENT_AWAIT_REPLY;
  136. rxrpc_notify_end_tx(rx, call, notify_end_tx);
  137. break;
  138. case RXRPC_CALL_SERVER_ACK_REQUEST:
  139. call->state = RXRPC_CALL_SERVER_SEND_REPLY;
  140. call->ack_at = call->expire_at;
  141. if (call->ackr_reason == RXRPC_ACK_DELAY)
  142. call->ackr_reason = 0;
  143. __rxrpc_set_timer(call, rxrpc_timer_init_for_send_reply,
  144. ktime_get_real());
  145. if (!last)
  146. break;
  147. case RXRPC_CALL_SERVER_SEND_REPLY:
  148. call->state = RXRPC_CALL_SERVER_AWAIT_ACK;
  149. rxrpc_notify_end_tx(rx, call, notify_end_tx);
  150. break;
  151. default:
  152. break;
  153. }
  154. write_unlock_bh(&call->state_lock);
  155. }
  156. if (seq == 1 && rxrpc_is_client_call(call))
  157. rxrpc_expose_client_call(call);
  158. ret = rxrpc_send_data_packet(call, skb, false);
  159. if (ret < 0) {
  160. _debug("need instant resend %d", ret);
  161. rxrpc_instant_resend(call, ix);
  162. } else {
  163. ktime_t now = ktime_get_real(), resend_at;
  164. resend_at = ktime_add_ms(now, rxrpc_resend_timeout);
  165. if (ktime_before(resend_at, call->resend_at)) {
  166. call->resend_at = resend_at;
  167. rxrpc_set_timer(call, rxrpc_timer_set_for_send, now);
  168. }
  169. }
  170. rxrpc_free_skb(skb, rxrpc_skb_tx_freed);
  171. _leave("");
  172. }
  173. /*
  174. * send data through a socket
  175. * - must be called in process context
  176. * - The caller holds the call user access mutex, but not the socket lock.
  177. */
  178. static int rxrpc_send_data(struct rxrpc_sock *rx,
  179. struct rxrpc_call *call,
  180. struct msghdr *msg, size_t len,
  181. rxrpc_notify_end_tx_t notify_end_tx)
  182. {
  183. struct rxrpc_skb_priv *sp;
  184. struct sk_buff *skb;
  185. struct sock *sk = &rx->sk;
  186. long timeo;
  187. bool more;
  188. int ret, copied;
  189. timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
  190. /* this should be in poll */
  191. sk_clear_bit(SOCKWQ_ASYNC_NOSPACE, sk);
  192. if (sk->sk_err || (sk->sk_shutdown & SEND_SHUTDOWN))
  193. return -EPIPE;
  194. more = msg->msg_flags & MSG_MORE;
  195. if (call->tx_total_len != -1) {
  196. if (len > call->tx_total_len)
  197. return -EMSGSIZE;
  198. if (!more && len != call->tx_total_len)
  199. return -EMSGSIZE;
  200. }
  201. skb = call->tx_pending;
  202. call->tx_pending = NULL;
  203. rxrpc_see_skb(skb, rxrpc_skb_tx_seen);
  204. copied = 0;
  205. do {
  206. /* Check to see if there's a ping ACK to reply to. */
  207. if (call->ackr_reason == RXRPC_ACK_PING_RESPONSE)
  208. rxrpc_send_ack_packet(call, false);
  209. if (!skb) {
  210. size_t size, chunk, max, space;
  211. _debug("alloc");
  212. if (call->tx_top - call->tx_hard_ack >=
  213. min_t(unsigned int, call->tx_winsize,
  214. call->cong_cwnd + call->cong_extra)) {
  215. ret = -EAGAIN;
  216. if (msg->msg_flags & MSG_DONTWAIT)
  217. goto maybe_error;
  218. ret = rxrpc_wait_for_tx_window(rx, call,
  219. &timeo);
  220. if (ret < 0)
  221. goto maybe_error;
  222. }
  223. max = RXRPC_JUMBO_DATALEN;
  224. max -= call->conn->security_size;
  225. max &= ~(call->conn->size_align - 1UL);
  226. chunk = max;
  227. if (chunk > msg_data_left(msg) && !more)
  228. chunk = msg_data_left(msg);
  229. space = chunk + call->conn->size_align;
  230. space &= ~(call->conn->size_align - 1UL);
  231. size = space + call->conn->security_size;
  232. _debug("SIZE: %zu/%zu/%zu", chunk, space, size);
  233. /* create a buffer that we can retain until it's ACK'd */
  234. skb = sock_alloc_send_skb(
  235. sk, size, msg->msg_flags & MSG_DONTWAIT, &ret);
  236. if (!skb)
  237. goto maybe_error;
  238. rxrpc_new_skb(skb, rxrpc_skb_tx_new);
  239. _debug("ALLOC SEND %p", skb);
  240. ASSERTCMP(skb->mark, ==, 0);
  241. _debug("HS: %u", call->conn->security_size);
  242. skb_reserve(skb, call->conn->security_size);
  243. skb->len += call->conn->security_size;
  244. sp = rxrpc_skb(skb);
  245. sp->remain = chunk;
  246. if (sp->remain > skb_tailroom(skb))
  247. sp->remain = skb_tailroom(skb);
  248. _net("skb: hr %d, tr %d, hl %d, rm %d",
  249. skb_headroom(skb),
  250. skb_tailroom(skb),
  251. skb_headlen(skb),
  252. sp->remain);
  253. skb->ip_summed = CHECKSUM_UNNECESSARY;
  254. }
  255. _debug("append");
  256. sp = rxrpc_skb(skb);
  257. /* append next segment of data to the current buffer */
  258. if (msg_data_left(msg) > 0) {
  259. int copy = skb_tailroom(skb);
  260. ASSERTCMP(copy, >, 0);
  261. if (copy > msg_data_left(msg))
  262. copy = msg_data_left(msg);
  263. if (copy > sp->remain)
  264. copy = sp->remain;
  265. _debug("add");
  266. ret = skb_add_data(skb, &msg->msg_iter, copy);
  267. _debug("added");
  268. if (ret < 0)
  269. goto efault;
  270. sp->remain -= copy;
  271. skb->mark += copy;
  272. copied += copy;
  273. if (call->tx_total_len != -1)
  274. call->tx_total_len -= copy;
  275. }
  276. /* add the packet to the send queue if it's now full */
  277. if (sp->remain <= 0 ||
  278. (msg_data_left(msg) == 0 && !more)) {
  279. struct rxrpc_connection *conn = call->conn;
  280. uint32_t seq;
  281. size_t pad;
  282. /* pad out if we're using security */
  283. if (conn->security_ix) {
  284. pad = conn->security_size + skb->mark;
  285. pad = conn->size_align - pad;
  286. pad &= conn->size_align - 1;
  287. _debug("pad %zu", pad);
  288. if (pad)
  289. skb_put_zero(skb, pad);
  290. }
  291. seq = call->tx_top + 1;
  292. sp->hdr.seq = seq;
  293. sp->hdr._rsvd = 0;
  294. sp->hdr.flags = conn->out_clientflag;
  295. if (msg_data_left(msg) == 0 && !more)
  296. sp->hdr.flags |= RXRPC_LAST_PACKET;
  297. else if (call->tx_top - call->tx_hard_ack <
  298. call->tx_winsize)
  299. sp->hdr.flags |= RXRPC_MORE_PACKETS;
  300. ret = conn->security->secure_packet(
  301. call, skb, skb->mark, skb->head);
  302. if (ret < 0)
  303. goto out;
  304. rxrpc_queue_packet(rx, call, skb,
  305. !msg_data_left(msg) && !more,
  306. notify_end_tx);
  307. skb = NULL;
  308. }
  309. /* Check for the far side aborting the call or a network error
  310. * occurring. If this happens, save any packet that was under
  311. * construction so that in the case of a network error, the
  312. * call can be retried or redirected.
  313. */
  314. if (call->state == RXRPC_CALL_COMPLETE) {
  315. ret = call->error;
  316. goto out;
  317. }
  318. } while (msg_data_left(msg) > 0);
  319. success:
  320. ret = copied;
  321. out:
  322. call->tx_pending = skb;
  323. _leave(" = %d", ret);
  324. return ret;
  325. maybe_error:
  326. if (copied)
  327. goto success;
  328. goto out;
  329. efault:
  330. ret = -EFAULT;
  331. goto out;
  332. }
  333. /*
  334. * extract control messages from the sendmsg() control buffer
  335. */
  336. static int rxrpc_sendmsg_cmsg(struct msghdr *msg, struct rxrpc_send_params *p)
  337. {
  338. struct cmsghdr *cmsg;
  339. bool got_user_ID = false;
  340. int len;
  341. if (msg->msg_controllen == 0)
  342. return -EINVAL;
  343. for_each_cmsghdr(cmsg, msg) {
  344. if (!CMSG_OK(msg, cmsg))
  345. return -EINVAL;
  346. len = cmsg->cmsg_len - sizeof(struct cmsghdr);
  347. _debug("CMSG %d, %d, %d",
  348. cmsg->cmsg_level, cmsg->cmsg_type, len);
  349. if (cmsg->cmsg_level != SOL_RXRPC)
  350. continue;
  351. switch (cmsg->cmsg_type) {
  352. case RXRPC_USER_CALL_ID:
  353. if (msg->msg_flags & MSG_CMSG_COMPAT) {
  354. if (len != sizeof(u32))
  355. return -EINVAL;
  356. p->user_call_ID = *(u32 *)CMSG_DATA(cmsg);
  357. } else {
  358. if (len != sizeof(unsigned long))
  359. return -EINVAL;
  360. p->user_call_ID = *(unsigned long *)
  361. CMSG_DATA(cmsg);
  362. }
  363. got_user_ID = true;
  364. break;
  365. case RXRPC_ABORT:
  366. if (p->command != RXRPC_CMD_SEND_DATA)
  367. return -EINVAL;
  368. p->command = RXRPC_CMD_SEND_ABORT;
  369. if (len != sizeof(p->abort_code))
  370. return -EINVAL;
  371. p->abort_code = *(unsigned int *)CMSG_DATA(cmsg);
  372. if (p->abort_code == 0)
  373. return -EINVAL;
  374. break;
  375. case RXRPC_ACCEPT:
  376. if (p->command != RXRPC_CMD_SEND_DATA)
  377. return -EINVAL;
  378. p->command = RXRPC_CMD_ACCEPT;
  379. if (len != 0)
  380. return -EINVAL;
  381. break;
  382. case RXRPC_EXCLUSIVE_CALL:
  383. p->exclusive = true;
  384. if (len != 0)
  385. return -EINVAL;
  386. break;
  387. case RXRPC_UPGRADE_SERVICE:
  388. p->upgrade = true;
  389. if (len != 0)
  390. return -EINVAL;
  391. break;
  392. case RXRPC_TX_LENGTH:
  393. if (p->tx_total_len != -1 || len != sizeof(__s64))
  394. return -EINVAL;
  395. p->tx_total_len = *(__s64 *)CMSG_DATA(cmsg);
  396. if (p->tx_total_len < 0)
  397. return -EINVAL;
  398. break;
  399. default:
  400. return -EINVAL;
  401. }
  402. }
  403. if (!got_user_ID)
  404. return -EINVAL;
  405. if (p->tx_total_len != -1 && p->command != RXRPC_CMD_SEND_DATA)
  406. return -EINVAL;
  407. _leave(" = 0");
  408. return 0;
  409. }
  410. /*
  411. * Create a new client call for sendmsg().
  412. * - Called with the socket lock held, which it must release.
  413. * - If it returns a call, the call's lock will need releasing by the caller.
  414. */
  415. static struct rxrpc_call *
  416. rxrpc_new_client_call_for_sendmsg(struct rxrpc_sock *rx, struct msghdr *msg,
  417. struct rxrpc_send_params *p)
  418. __releases(&rx->sk.sk_lock.slock)
  419. {
  420. struct rxrpc_conn_parameters cp;
  421. struct rxrpc_call *call;
  422. struct key *key;
  423. DECLARE_SOCKADDR(struct sockaddr_rxrpc *, srx, msg->msg_name);
  424. _enter("");
  425. if (!msg->msg_name) {
  426. release_sock(&rx->sk);
  427. return ERR_PTR(-EDESTADDRREQ);
  428. }
  429. key = rx->key;
  430. if (key && !rx->key->payload.data[0])
  431. key = NULL;
  432. memset(&cp, 0, sizeof(cp));
  433. cp.local = rx->local;
  434. cp.key = rx->key;
  435. cp.security_level = rx->min_sec_level;
  436. cp.exclusive = rx->exclusive | p->exclusive;
  437. cp.upgrade = p->upgrade;
  438. cp.service_id = srx->srx_service;
  439. call = rxrpc_new_client_call(rx, &cp, srx, p->user_call_ID,
  440. p->tx_total_len, GFP_KERNEL);
  441. /* The socket is now unlocked */
  442. _leave(" = %p\n", call);
  443. return call;
  444. }
  445. /*
  446. * send a message forming part of a client call through an RxRPC socket
  447. * - caller holds the socket locked
  448. * - the socket may be either a client socket or a server socket
  449. */
  450. int rxrpc_do_sendmsg(struct rxrpc_sock *rx, struct msghdr *msg, size_t len)
  451. __releases(&rx->sk.sk_lock.slock)
  452. {
  453. enum rxrpc_call_state state;
  454. struct rxrpc_call *call;
  455. int ret;
  456. struct rxrpc_send_params p = {
  457. .tx_total_len = -1,
  458. .user_call_ID = 0,
  459. .abort_code = 0,
  460. .command = RXRPC_CMD_SEND_DATA,
  461. .exclusive = false,
  462. .upgrade = true,
  463. };
  464. _enter("");
  465. ret = rxrpc_sendmsg_cmsg(msg, &p);
  466. if (ret < 0)
  467. goto error_release_sock;
  468. if (p.command == RXRPC_CMD_ACCEPT) {
  469. ret = -EINVAL;
  470. if (rx->sk.sk_state != RXRPC_SERVER_LISTENING)
  471. goto error_release_sock;
  472. call = rxrpc_accept_call(rx, p.user_call_ID, NULL);
  473. /* The socket is now unlocked. */
  474. if (IS_ERR(call))
  475. return PTR_ERR(call);
  476. rxrpc_put_call(call, rxrpc_call_put);
  477. return 0;
  478. }
  479. call = rxrpc_find_call_by_user_ID(rx, p.user_call_ID);
  480. if (!call) {
  481. ret = -EBADSLT;
  482. if (p.command != RXRPC_CMD_SEND_DATA)
  483. goto error_release_sock;
  484. call = rxrpc_new_client_call_for_sendmsg(rx, msg, &p);
  485. /* The socket is now unlocked... */
  486. if (IS_ERR(call))
  487. return PTR_ERR(call);
  488. /* ... and we have the call lock. */
  489. } else {
  490. switch (READ_ONCE(call->state)) {
  491. case RXRPC_CALL_UNINITIALISED:
  492. case RXRPC_CALL_CLIENT_AWAIT_CONN:
  493. case RXRPC_CALL_SERVER_PREALLOC:
  494. case RXRPC_CALL_SERVER_SECURING:
  495. case RXRPC_CALL_SERVER_ACCEPTING:
  496. ret = -EBUSY;
  497. goto error_release_sock;
  498. default:
  499. break;
  500. }
  501. ret = mutex_lock_interruptible(&call->user_mutex);
  502. release_sock(&rx->sk);
  503. if (ret < 0) {
  504. ret = -ERESTARTSYS;
  505. goto error_put;
  506. }
  507. if (p.tx_total_len != -1) {
  508. ret = -EINVAL;
  509. if (call->tx_total_len != -1 ||
  510. call->tx_pending ||
  511. call->tx_top != 0)
  512. goto error_put;
  513. call->tx_total_len = p.tx_total_len;
  514. }
  515. }
  516. state = READ_ONCE(call->state);
  517. _debug("CALL %d USR %lx ST %d on CONN %p",
  518. call->debug_id, call->user_call_ID, state, call->conn);
  519. if (state >= RXRPC_CALL_COMPLETE) {
  520. /* it's too late for this call */
  521. ret = -ESHUTDOWN;
  522. } else if (p.command == RXRPC_CMD_SEND_ABORT) {
  523. ret = 0;
  524. if (rxrpc_abort_call("CMD", call, 0, p.abort_code, -ECONNABORTED))
  525. ret = rxrpc_send_abort_packet(call);
  526. } else if (p.command != RXRPC_CMD_SEND_DATA) {
  527. ret = -EINVAL;
  528. } else if (rxrpc_is_client_call(call) &&
  529. state != RXRPC_CALL_CLIENT_SEND_REQUEST) {
  530. /* request phase complete for this client call */
  531. ret = -EPROTO;
  532. } else if (rxrpc_is_service_call(call) &&
  533. state != RXRPC_CALL_SERVER_ACK_REQUEST &&
  534. state != RXRPC_CALL_SERVER_SEND_REPLY) {
  535. /* Reply phase not begun or not complete for service call. */
  536. ret = -EPROTO;
  537. } else {
  538. ret = rxrpc_send_data(rx, call, msg, len, NULL);
  539. }
  540. mutex_unlock(&call->user_mutex);
  541. error_put:
  542. rxrpc_put_call(call, rxrpc_call_put);
  543. _leave(" = %d", ret);
  544. return ret;
  545. error_release_sock:
  546. release_sock(&rx->sk);
  547. return ret;
  548. }
  549. /**
  550. * rxrpc_kernel_send_data - Allow a kernel service to send data on a call
  551. * @sock: The socket the call is on
  552. * @call: The call to send data through
  553. * @msg: The data to send
  554. * @len: The amount of data to send
  555. * @notify_end_tx: Notification that the last packet is queued.
  556. *
  557. * Allow a kernel service to send data on a call. The call must be in an state
  558. * appropriate to sending data. No control data should be supplied in @msg,
  559. * nor should an address be supplied. MSG_MORE should be flagged if there's
  560. * more data to come, otherwise this data will end the transmission phase.
  561. */
  562. int rxrpc_kernel_send_data(struct socket *sock, struct rxrpc_call *call,
  563. struct msghdr *msg, size_t len,
  564. rxrpc_notify_end_tx_t notify_end_tx)
  565. {
  566. int ret;
  567. _enter("{%d,%s},", call->debug_id, rxrpc_call_states[call->state]);
  568. ASSERTCMP(msg->msg_name, ==, NULL);
  569. ASSERTCMP(msg->msg_control, ==, NULL);
  570. mutex_lock(&call->user_mutex);
  571. _debug("CALL %d USR %lx ST %d on CONN %p",
  572. call->debug_id, call->user_call_ID, call->state, call->conn);
  573. switch (READ_ONCE(call->state)) {
  574. case RXRPC_CALL_CLIENT_SEND_REQUEST:
  575. case RXRPC_CALL_SERVER_ACK_REQUEST:
  576. case RXRPC_CALL_SERVER_SEND_REPLY:
  577. ret = rxrpc_send_data(rxrpc_sk(sock->sk), call, msg, len,
  578. notify_end_tx);
  579. break;
  580. case RXRPC_CALL_COMPLETE:
  581. read_lock_bh(&call->state_lock);
  582. ret = call->error;
  583. read_unlock_bh(&call->state_lock);
  584. break;
  585. default:
  586. /* Request phase complete for this client call */
  587. trace_rxrpc_rx_eproto(call, 0, tracepoint_string("late_send"));
  588. ret = -EPROTO;
  589. break;
  590. }
  591. mutex_unlock(&call->user_mutex);
  592. _leave(" = %d", ret);
  593. return ret;
  594. }
  595. EXPORT_SYMBOL(rxrpc_kernel_send_data);
  596. /**
  597. * rxrpc_kernel_abort_call - Allow a kernel service to abort a call
  598. * @sock: The socket the call is on
  599. * @call: The call to be aborted
  600. * @abort_code: The abort code to stick into the ABORT packet
  601. * @error: Local error value
  602. * @why: 3-char string indicating why.
  603. *
  604. * Allow a kernel service to abort a call, if it's still in an abortable state
  605. * and return true if the call was aborted, false if it was already complete.
  606. */
  607. bool rxrpc_kernel_abort_call(struct socket *sock, struct rxrpc_call *call,
  608. u32 abort_code, int error, const char *why)
  609. {
  610. bool aborted;
  611. _enter("{%d},%d,%d,%s", call->debug_id, abort_code, error, why);
  612. mutex_lock(&call->user_mutex);
  613. aborted = rxrpc_abort_call(why, call, 0, abort_code, error);
  614. if (aborted)
  615. rxrpc_send_abort_packet(call);
  616. mutex_unlock(&call->user_mutex);
  617. return aborted;
  618. }
  619. EXPORT_SYMBOL(rxrpc_kernel_abort_call);
  620. /**
  621. * rxrpc_kernel_set_tx_length - Set the total Tx length on a call
  622. * @sock: The socket the call is on
  623. * @call: The call to be informed
  624. * @tx_total_len: The amount of data to be transmitted for this call
  625. *
  626. * Allow a kernel service to set the total transmit length on a call. This
  627. * allows buffer-to-packet encrypt-and-copy to be performed.
  628. *
  629. * This function is primarily for use for setting the reply length since the
  630. * request length can be set when beginning the call.
  631. */
  632. void rxrpc_kernel_set_tx_length(struct socket *sock, struct rxrpc_call *call,
  633. s64 tx_total_len)
  634. {
  635. WARN_ON(call->tx_total_len != -1);
  636. call->tx_total_len = tx_total_len;
  637. }
  638. EXPORT_SYMBOL(rxrpc_kernel_set_tx_length);