sendmsg.c 22 KB

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