sendmsg.c 21 KB

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