sendmsg.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840
  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. __acquires(&call->user_mutex)
  470. {
  471. struct rxrpc_conn_parameters cp;
  472. struct rxrpc_call *call;
  473. struct key *key;
  474. DECLARE_SOCKADDR(struct sockaddr_rxrpc *, srx, msg->msg_name);
  475. _enter("");
  476. if (!msg->msg_name) {
  477. release_sock(&rx->sk);
  478. return ERR_PTR(-EDESTADDRREQ);
  479. }
  480. key = rx->key;
  481. if (key && !rx->key->payload.data[0])
  482. key = NULL;
  483. memset(&cp, 0, sizeof(cp));
  484. cp.local = rx->local;
  485. cp.key = rx->key;
  486. cp.security_level = rx->min_sec_level;
  487. cp.exclusive = rx->exclusive | p->exclusive;
  488. cp.upgrade = p->upgrade;
  489. cp.service_id = srx->srx_service;
  490. call = rxrpc_new_client_call(rx, &cp, srx, &p->call, GFP_KERNEL,
  491. atomic_inc_return(&rxrpc_debug_id));
  492. /* The socket is now unlocked */
  493. rxrpc_put_peer(cp.peer);
  494. _leave(" = %p\n", call);
  495. return call;
  496. }
  497. /*
  498. * send a message forming part of a client call through an RxRPC socket
  499. * - caller holds the socket locked
  500. * - the socket may be either a client socket or a server socket
  501. */
  502. int rxrpc_do_sendmsg(struct rxrpc_sock *rx, struct msghdr *msg, size_t len)
  503. __releases(&rx->sk.sk_lock.slock)
  504. __releases(&call->user_mutex)
  505. {
  506. enum rxrpc_call_state state;
  507. struct rxrpc_call *call;
  508. unsigned long now, j;
  509. int ret;
  510. struct rxrpc_send_params p = {
  511. .call.tx_total_len = -1,
  512. .call.user_call_ID = 0,
  513. .call.nr_timeouts = 0,
  514. .abort_code = 0,
  515. .command = RXRPC_CMD_SEND_DATA,
  516. .exclusive = false,
  517. .upgrade = false,
  518. };
  519. _enter("");
  520. ret = rxrpc_sendmsg_cmsg(msg, &p);
  521. if (ret < 0)
  522. goto error_release_sock;
  523. if (p.command == RXRPC_CMD_ACCEPT) {
  524. ret = -EINVAL;
  525. if (rx->sk.sk_state != RXRPC_SERVER_LISTENING)
  526. goto error_release_sock;
  527. call = rxrpc_accept_call(rx, p.call.user_call_ID, NULL);
  528. /* The socket is now unlocked. */
  529. if (IS_ERR(call))
  530. return PTR_ERR(call);
  531. ret = 0;
  532. goto out_put_unlock;
  533. }
  534. call = rxrpc_find_call_by_user_ID(rx, p.call.user_call_ID);
  535. if (!call) {
  536. ret = -EBADSLT;
  537. if (p.command != RXRPC_CMD_SEND_DATA)
  538. goto error_release_sock;
  539. call = rxrpc_new_client_call_for_sendmsg(rx, msg, &p);
  540. /* The socket is now unlocked... */
  541. if (IS_ERR(call))
  542. return PTR_ERR(call);
  543. /* ... and we have the call lock. */
  544. } else {
  545. switch (READ_ONCE(call->state)) {
  546. case RXRPC_CALL_UNINITIALISED:
  547. case RXRPC_CALL_CLIENT_AWAIT_CONN:
  548. case RXRPC_CALL_SERVER_PREALLOC:
  549. case RXRPC_CALL_SERVER_SECURING:
  550. case RXRPC_CALL_SERVER_ACCEPTING:
  551. ret = -EBUSY;
  552. goto error_release_sock;
  553. default:
  554. break;
  555. }
  556. ret = mutex_lock_interruptible(&call->user_mutex);
  557. release_sock(&rx->sk);
  558. if (ret < 0) {
  559. ret = -ERESTARTSYS;
  560. goto error_put;
  561. }
  562. if (p.call.tx_total_len != -1) {
  563. ret = -EINVAL;
  564. if (call->tx_total_len != -1 ||
  565. call->tx_pending ||
  566. call->tx_top != 0)
  567. goto error_put;
  568. call->tx_total_len = p.call.tx_total_len;
  569. }
  570. }
  571. switch (p.call.nr_timeouts) {
  572. case 3:
  573. j = msecs_to_jiffies(p.call.timeouts.normal);
  574. if (p.call.timeouts.normal > 0 && j == 0)
  575. j = 1;
  576. WRITE_ONCE(call->next_rx_timo, j);
  577. /* Fall through */
  578. case 2:
  579. j = msecs_to_jiffies(p.call.timeouts.idle);
  580. if (p.call.timeouts.idle > 0 && j == 0)
  581. j = 1;
  582. WRITE_ONCE(call->next_req_timo, j);
  583. /* Fall through */
  584. case 1:
  585. if (p.call.timeouts.hard > 0) {
  586. j = msecs_to_jiffies(p.call.timeouts.hard);
  587. now = jiffies;
  588. j += now;
  589. WRITE_ONCE(call->expect_term_by, j);
  590. rxrpc_reduce_call_timer(call, j, now,
  591. rxrpc_timer_set_for_hard);
  592. }
  593. break;
  594. }
  595. state = READ_ONCE(call->state);
  596. _debug("CALL %d USR %lx ST %d on CONN %p",
  597. call->debug_id, call->user_call_ID, state, call->conn);
  598. if (state >= RXRPC_CALL_COMPLETE) {
  599. /* it's too late for this call */
  600. ret = -ESHUTDOWN;
  601. } else if (p.command == RXRPC_CMD_SEND_ABORT) {
  602. ret = 0;
  603. if (rxrpc_abort_call("CMD", call, 0, p.abort_code, -ECONNABORTED))
  604. ret = rxrpc_send_abort_packet(call);
  605. } else if (p.command != RXRPC_CMD_SEND_DATA) {
  606. ret = -EINVAL;
  607. } else if (rxrpc_is_client_call(call) &&
  608. state != RXRPC_CALL_CLIENT_SEND_REQUEST) {
  609. /* request phase complete for this client call */
  610. ret = -EPROTO;
  611. } else if (rxrpc_is_service_call(call) &&
  612. state != RXRPC_CALL_SERVER_ACK_REQUEST &&
  613. state != RXRPC_CALL_SERVER_SEND_REPLY) {
  614. /* Reply phase not begun or not complete for service call. */
  615. ret = -EPROTO;
  616. } else {
  617. ret = rxrpc_send_data(rx, call, msg, len, NULL);
  618. }
  619. out_put_unlock:
  620. mutex_unlock(&call->user_mutex);
  621. error_put:
  622. rxrpc_put_call(call, rxrpc_call_put);
  623. _leave(" = %d", ret);
  624. return ret;
  625. error_release_sock:
  626. release_sock(&rx->sk);
  627. return ret;
  628. }
  629. /**
  630. * rxrpc_kernel_send_data - Allow a kernel service to send data on a call
  631. * @sock: The socket the call is on
  632. * @call: The call to send data through
  633. * @msg: The data to send
  634. * @len: The amount of data to send
  635. * @notify_end_tx: Notification that the last packet is queued.
  636. *
  637. * Allow a kernel service to send data on a call. The call must be in an state
  638. * appropriate to sending data. No control data should be supplied in @msg,
  639. * nor should an address be supplied. MSG_MORE should be flagged if there's
  640. * more data to come, otherwise this data will end the transmission phase.
  641. */
  642. int rxrpc_kernel_send_data(struct socket *sock, struct rxrpc_call *call,
  643. struct msghdr *msg, size_t len,
  644. rxrpc_notify_end_tx_t notify_end_tx)
  645. {
  646. int ret;
  647. _enter("{%d,%s},", call->debug_id, rxrpc_call_states[call->state]);
  648. ASSERTCMP(msg->msg_name, ==, NULL);
  649. ASSERTCMP(msg->msg_control, ==, NULL);
  650. mutex_lock(&call->user_mutex);
  651. _debug("CALL %d USR %lx ST %d on CONN %p",
  652. call->debug_id, call->user_call_ID, call->state, call->conn);
  653. switch (READ_ONCE(call->state)) {
  654. case RXRPC_CALL_CLIENT_SEND_REQUEST:
  655. case RXRPC_CALL_SERVER_ACK_REQUEST:
  656. case RXRPC_CALL_SERVER_SEND_REPLY:
  657. ret = rxrpc_send_data(rxrpc_sk(sock->sk), call, msg, len,
  658. notify_end_tx);
  659. break;
  660. case RXRPC_CALL_COMPLETE:
  661. read_lock_bh(&call->state_lock);
  662. ret = call->error;
  663. read_unlock_bh(&call->state_lock);
  664. break;
  665. default:
  666. /* Request phase complete for this client call */
  667. trace_rxrpc_rx_eproto(call, 0, tracepoint_string("late_send"));
  668. ret = -EPROTO;
  669. break;
  670. }
  671. mutex_unlock(&call->user_mutex);
  672. _leave(" = %d", ret);
  673. return ret;
  674. }
  675. EXPORT_SYMBOL(rxrpc_kernel_send_data);
  676. /**
  677. * rxrpc_kernel_abort_call - Allow a kernel service to abort a call
  678. * @sock: The socket the call is on
  679. * @call: The call to be aborted
  680. * @abort_code: The abort code to stick into the ABORT packet
  681. * @error: Local error value
  682. * @why: 3-char string indicating why.
  683. *
  684. * Allow a kernel service to abort a call, if it's still in an abortable state
  685. * and return true if the call was aborted, false if it was already complete.
  686. */
  687. bool rxrpc_kernel_abort_call(struct socket *sock, struct rxrpc_call *call,
  688. u32 abort_code, int error, const char *why)
  689. {
  690. bool aborted;
  691. _enter("{%d},%d,%d,%s", call->debug_id, abort_code, error, why);
  692. mutex_lock(&call->user_mutex);
  693. aborted = rxrpc_abort_call(why, call, 0, abort_code, error);
  694. if (aborted)
  695. rxrpc_send_abort_packet(call);
  696. mutex_unlock(&call->user_mutex);
  697. return aborted;
  698. }
  699. EXPORT_SYMBOL(rxrpc_kernel_abort_call);
  700. /**
  701. * rxrpc_kernel_set_tx_length - Set the total Tx length on a call
  702. * @sock: The socket the call is on
  703. * @call: The call to be informed
  704. * @tx_total_len: The amount of data to be transmitted for this call
  705. *
  706. * Allow a kernel service to set the total transmit length on a call. This
  707. * allows buffer-to-packet encrypt-and-copy to be performed.
  708. *
  709. * This function is primarily for use for setting the reply length since the
  710. * request length can be set when beginning the call.
  711. */
  712. void rxrpc_kernel_set_tx_length(struct socket *sock, struct rxrpc_call *call,
  713. s64 tx_total_len)
  714. {
  715. WARN_ON(call->tx_total_len != -1);
  716. call->tx_total_len = tx_total_len;
  717. }
  718. EXPORT_SYMBOL(rxrpc_kernel_set_tx_length);