sendmsg.c 18 KB

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