sendmsg.c 22 KB

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