sendmsg.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855
  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 < 2)
  57. rtt2 = 2;
  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. switch (ret) {
  193. case -ENETUNREACH:
  194. case -EHOSTUNREACH:
  195. case -ECONNREFUSED:
  196. rxrpc_set_call_completion(call,
  197. RXRPC_CALL_LOCAL_ERROR,
  198. 0, ret);
  199. rxrpc_notify_socket(call);
  200. goto out;
  201. }
  202. _debug("need instant resend %d", ret);
  203. rxrpc_instant_resend(call, ix);
  204. } else {
  205. unsigned long now = jiffies, resend_at;
  206. if (call->peer->rtt_usage > 1)
  207. resend_at = nsecs_to_jiffies(call->peer->rtt * 3 / 2);
  208. else
  209. resend_at = rxrpc_resend_timeout;
  210. if (resend_at < 1)
  211. resend_at = 1;
  212. resend_at += now;
  213. WRITE_ONCE(call->resend_at, resend_at);
  214. rxrpc_reduce_call_timer(call, resend_at, now,
  215. rxrpc_timer_set_for_send);
  216. }
  217. out:
  218. rxrpc_free_skb(skb, rxrpc_skb_tx_freed);
  219. _leave("");
  220. }
  221. /*
  222. * send data through a socket
  223. * - must be called in process context
  224. * - The caller holds the call user access mutex, but not the socket lock.
  225. */
  226. static int rxrpc_send_data(struct rxrpc_sock *rx,
  227. struct rxrpc_call *call,
  228. struct msghdr *msg, size_t len,
  229. rxrpc_notify_end_tx_t notify_end_tx)
  230. {
  231. struct rxrpc_skb_priv *sp;
  232. struct sk_buff *skb;
  233. struct sock *sk = &rx->sk;
  234. long timeo;
  235. bool more;
  236. int ret, copied;
  237. timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
  238. /* this should be in poll */
  239. sk_clear_bit(SOCKWQ_ASYNC_NOSPACE, sk);
  240. if (sk->sk_shutdown & SEND_SHUTDOWN)
  241. return -EPIPE;
  242. more = msg->msg_flags & MSG_MORE;
  243. if (call->tx_total_len != -1) {
  244. if (len > call->tx_total_len)
  245. return -EMSGSIZE;
  246. if (!more && len != call->tx_total_len)
  247. return -EMSGSIZE;
  248. }
  249. skb = call->tx_pending;
  250. call->tx_pending = NULL;
  251. rxrpc_see_skb(skb, rxrpc_skb_tx_seen);
  252. copied = 0;
  253. do {
  254. /* Check to see if there's a ping ACK to reply to. */
  255. if (call->ackr_reason == RXRPC_ACK_PING_RESPONSE)
  256. rxrpc_send_ack_packet(call, false, NULL);
  257. if (!skb) {
  258. size_t size, chunk, max, space;
  259. _debug("alloc");
  260. if (call->tx_top - call->tx_hard_ack >=
  261. min_t(unsigned int, call->tx_winsize,
  262. call->cong_cwnd + call->cong_extra)) {
  263. ret = -EAGAIN;
  264. if (msg->msg_flags & MSG_DONTWAIT)
  265. goto maybe_error;
  266. ret = rxrpc_wait_for_tx_window(rx, call,
  267. &timeo,
  268. msg->msg_flags & MSG_WAITALL);
  269. if (ret < 0)
  270. goto maybe_error;
  271. }
  272. max = RXRPC_JUMBO_DATALEN;
  273. max -= call->conn->security_size;
  274. max &= ~(call->conn->size_align - 1UL);
  275. chunk = max;
  276. if (chunk > msg_data_left(msg) && !more)
  277. chunk = msg_data_left(msg);
  278. space = chunk + call->conn->size_align;
  279. space &= ~(call->conn->size_align - 1UL);
  280. size = space + call->conn->security_size;
  281. _debug("SIZE: %zu/%zu/%zu", chunk, space, size);
  282. /* create a buffer that we can retain until it's ACK'd */
  283. skb = sock_alloc_send_skb(
  284. sk, size, msg->msg_flags & MSG_DONTWAIT, &ret);
  285. if (!skb)
  286. goto maybe_error;
  287. rxrpc_new_skb(skb, rxrpc_skb_tx_new);
  288. _debug("ALLOC SEND %p", skb);
  289. ASSERTCMP(skb->mark, ==, 0);
  290. _debug("HS: %u", call->conn->security_size);
  291. skb_reserve(skb, call->conn->security_size);
  292. skb->len += call->conn->security_size;
  293. sp = rxrpc_skb(skb);
  294. sp->remain = chunk;
  295. if (sp->remain > skb_tailroom(skb))
  296. sp->remain = skb_tailroom(skb);
  297. _net("skb: hr %d, tr %d, hl %d, rm %d",
  298. skb_headroom(skb),
  299. skb_tailroom(skb),
  300. skb_headlen(skb),
  301. sp->remain);
  302. skb->ip_summed = CHECKSUM_UNNECESSARY;
  303. }
  304. _debug("append");
  305. sp = rxrpc_skb(skb);
  306. /* append next segment of data to the current buffer */
  307. if (msg_data_left(msg) > 0) {
  308. int copy = skb_tailroom(skb);
  309. ASSERTCMP(copy, >, 0);
  310. if (copy > msg_data_left(msg))
  311. copy = msg_data_left(msg);
  312. if (copy > sp->remain)
  313. copy = sp->remain;
  314. _debug("add");
  315. ret = skb_add_data(skb, &msg->msg_iter, copy);
  316. _debug("added");
  317. if (ret < 0)
  318. goto efault;
  319. sp->remain -= copy;
  320. skb->mark += copy;
  321. copied += copy;
  322. if (call->tx_total_len != -1)
  323. call->tx_total_len -= copy;
  324. }
  325. /* add the packet to the send queue if it's now full */
  326. if (sp->remain <= 0 ||
  327. (msg_data_left(msg) == 0 && !more)) {
  328. struct rxrpc_connection *conn = call->conn;
  329. uint32_t seq;
  330. size_t pad;
  331. /* pad out if we're using security */
  332. if (conn->security_ix) {
  333. pad = conn->security_size + skb->mark;
  334. pad = conn->size_align - pad;
  335. pad &= conn->size_align - 1;
  336. _debug("pad %zu", pad);
  337. if (pad)
  338. skb_put_zero(skb, pad);
  339. }
  340. seq = call->tx_top + 1;
  341. sp->hdr.seq = seq;
  342. sp->hdr._rsvd = 0;
  343. sp->hdr.flags = conn->out_clientflag;
  344. if (msg_data_left(msg) == 0 && !more)
  345. sp->hdr.flags |= RXRPC_LAST_PACKET;
  346. else if (call->tx_top - call->tx_hard_ack <
  347. call->tx_winsize)
  348. sp->hdr.flags |= RXRPC_MORE_PACKETS;
  349. ret = conn->security->secure_packet(
  350. call, skb, skb->mark, skb->head);
  351. if (ret < 0)
  352. goto out;
  353. rxrpc_queue_packet(rx, call, skb,
  354. !msg_data_left(msg) && !more,
  355. notify_end_tx);
  356. skb = NULL;
  357. }
  358. /* Check for the far side aborting the call or a network error
  359. * occurring. If this happens, save any packet that was under
  360. * construction so that in the case of a network error, the
  361. * call can be retried or redirected.
  362. */
  363. if (call->state == RXRPC_CALL_COMPLETE) {
  364. ret = call->error;
  365. goto out;
  366. }
  367. } while (msg_data_left(msg) > 0);
  368. success:
  369. ret = copied;
  370. out:
  371. call->tx_pending = skb;
  372. _leave(" = %d", ret);
  373. return ret;
  374. maybe_error:
  375. if (copied)
  376. goto success;
  377. goto out;
  378. efault:
  379. ret = -EFAULT;
  380. goto out;
  381. }
  382. /*
  383. * extract control messages from the sendmsg() control buffer
  384. */
  385. static int rxrpc_sendmsg_cmsg(struct msghdr *msg, struct rxrpc_send_params *p)
  386. {
  387. struct cmsghdr *cmsg;
  388. bool got_user_ID = false;
  389. int len;
  390. if (msg->msg_controllen == 0)
  391. return -EINVAL;
  392. for_each_cmsghdr(cmsg, msg) {
  393. if (!CMSG_OK(msg, cmsg))
  394. return -EINVAL;
  395. len = cmsg->cmsg_len - sizeof(struct cmsghdr);
  396. _debug("CMSG %d, %d, %d",
  397. cmsg->cmsg_level, cmsg->cmsg_type, len);
  398. if (cmsg->cmsg_level != SOL_RXRPC)
  399. continue;
  400. switch (cmsg->cmsg_type) {
  401. case RXRPC_USER_CALL_ID:
  402. if (msg->msg_flags & MSG_CMSG_COMPAT) {
  403. if (len != sizeof(u32))
  404. return -EINVAL;
  405. p->call.user_call_ID = *(u32 *)CMSG_DATA(cmsg);
  406. } else {
  407. if (len != sizeof(unsigned long))
  408. return -EINVAL;
  409. p->call.user_call_ID = *(unsigned long *)
  410. CMSG_DATA(cmsg);
  411. }
  412. got_user_ID = true;
  413. break;
  414. case RXRPC_ABORT:
  415. if (p->command != RXRPC_CMD_SEND_DATA)
  416. return -EINVAL;
  417. p->command = RXRPC_CMD_SEND_ABORT;
  418. if (len != sizeof(p->abort_code))
  419. return -EINVAL;
  420. p->abort_code = *(unsigned int *)CMSG_DATA(cmsg);
  421. if (p->abort_code == 0)
  422. return -EINVAL;
  423. break;
  424. case RXRPC_ACCEPT:
  425. if (p->command != RXRPC_CMD_SEND_DATA)
  426. return -EINVAL;
  427. p->command = RXRPC_CMD_ACCEPT;
  428. if (len != 0)
  429. return -EINVAL;
  430. break;
  431. case RXRPC_EXCLUSIVE_CALL:
  432. p->exclusive = true;
  433. if (len != 0)
  434. return -EINVAL;
  435. break;
  436. case RXRPC_UPGRADE_SERVICE:
  437. p->upgrade = true;
  438. if (len != 0)
  439. return -EINVAL;
  440. break;
  441. case RXRPC_TX_LENGTH:
  442. if (p->call.tx_total_len != -1 || len != sizeof(__s64))
  443. return -EINVAL;
  444. p->call.tx_total_len = *(__s64 *)CMSG_DATA(cmsg);
  445. if (p->call.tx_total_len < 0)
  446. return -EINVAL;
  447. break;
  448. case RXRPC_SET_CALL_TIMEOUT:
  449. if (len & 3 || len < 4 || len > 12)
  450. return -EINVAL;
  451. memcpy(&p->call.timeouts, CMSG_DATA(cmsg), len);
  452. p->call.nr_timeouts = len / 4;
  453. if (p->call.timeouts.hard > INT_MAX / HZ)
  454. return -ERANGE;
  455. if (p->call.nr_timeouts >= 2 && p->call.timeouts.idle > 60 * 60 * 1000)
  456. return -ERANGE;
  457. if (p->call.nr_timeouts >= 3 && p->call.timeouts.normal > 60 * 60 * 1000)
  458. return -ERANGE;
  459. break;
  460. default:
  461. return -EINVAL;
  462. }
  463. }
  464. if (!got_user_ID)
  465. return -EINVAL;
  466. if (p->call.tx_total_len != -1 && p->command != RXRPC_CMD_SEND_DATA)
  467. return -EINVAL;
  468. _leave(" = 0");
  469. return 0;
  470. }
  471. /*
  472. * Create a new client call for sendmsg().
  473. * - Called with the socket lock held, which it must release.
  474. * - If it returns a call, the call's lock will need releasing by the caller.
  475. */
  476. static struct rxrpc_call *
  477. rxrpc_new_client_call_for_sendmsg(struct rxrpc_sock *rx, struct msghdr *msg,
  478. struct rxrpc_send_params *p)
  479. __releases(&rx->sk.sk_lock.slock)
  480. __acquires(&call->user_mutex)
  481. {
  482. struct rxrpc_conn_parameters cp;
  483. struct rxrpc_call *call;
  484. struct key *key;
  485. DECLARE_SOCKADDR(struct sockaddr_rxrpc *, srx, msg->msg_name);
  486. _enter("");
  487. if (!msg->msg_name) {
  488. release_sock(&rx->sk);
  489. return ERR_PTR(-EDESTADDRREQ);
  490. }
  491. key = rx->key;
  492. if (key && !rx->key->payload.data[0])
  493. key = NULL;
  494. memset(&cp, 0, sizeof(cp));
  495. cp.local = rx->local;
  496. cp.key = rx->key;
  497. cp.security_level = rx->min_sec_level;
  498. cp.exclusive = rx->exclusive | p->exclusive;
  499. cp.upgrade = p->upgrade;
  500. cp.service_id = srx->srx_service;
  501. call = rxrpc_new_client_call(rx, &cp, srx, &p->call, GFP_KERNEL,
  502. atomic_inc_return(&rxrpc_debug_id));
  503. /* The socket is now unlocked */
  504. rxrpc_put_peer(cp.peer);
  505. _leave(" = %p\n", call);
  506. return call;
  507. }
  508. /*
  509. * send a message forming part of a client call through an RxRPC socket
  510. * - caller holds the socket locked
  511. * - the socket may be either a client socket or a server socket
  512. */
  513. int rxrpc_do_sendmsg(struct rxrpc_sock *rx, struct msghdr *msg, size_t len)
  514. __releases(&rx->sk.sk_lock.slock)
  515. __releases(&call->user_mutex)
  516. {
  517. enum rxrpc_call_state state;
  518. struct rxrpc_call *call;
  519. unsigned long now, j;
  520. int ret;
  521. struct rxrpc_send_params p = {
  522. .call.tx_total_len = -1,
  523. .call.user_call_ID = 0,
  524. .call.nr_timeouts = 0,
  525. .abort_code = 0,
  526. .command = RXRPC_CMD_SEND_DATA,
  527. .exclusive = false,
  528. .upgrade = false,
  529. };
  530. _enter("");
  531. ret = rxrpc_sendmsg_cmsg(msg, &p);
  532. if (ret < 0)
  533. goto error_release_sock;
  534. if (p.command == RXRPC_CMD_ACCEPT) {
  535. ret = -EINVAL;
  536. if (rx->sk.sk_state != RXRPC_SERVER_LISTENING)
  537. goto error_release_sock;
  538. call = rxrpc_accept_call(rx, p.call.user_call_ID, NULL);
  539. /* The socket is now unlocked. */
  540. if (IS_ERR(call))
  541. return PTR_ERR(call);
  542. ret = 0;
  543. goto out_put_unlock;
  544. }
  545. call = rxrpc_find_call_by_user_ID(rx, p.call.user_call_ID);
  546. if (!call) {
  547. ret = -EBADSLT;
  548. if (p.command != RXRPC_CMD_SEND_DATA)
  549. goto error_release_sock;
  550. call = rxrpc_new_client_call_for_sendmsg(rx, msg, &p);
  551. /* The socket is now unlocked... */
  552. if (IS_ERR(call))
  553. return PTR_ERR(call);
  554. /* ... and we have the call lock. */
  555. ret = 0;
  556. if (READ_ONCE(call->state) == RXRPC_CALL_COMPLETE)
  557. goto out_put_unlock;
  558. } else {
  559. switch (READ_ONCE(call->state)) {
  560. case RXRPC_CALL_UNINITIALISED:
  561. case RXRPC_CALL_CLIENT_AWAIT_CONN:
  562. case RXRPC_CALL_SERVER_PREALLOC:
  563. case RXRPC_CALL_SERVER_SECURING:
  564. case RXRPC_CALL_SERVER_ACCEPTING:
  565. rxrpc_put_call(call, rxrpc_call_put);
  566. ret = -EBUSY;
  567. goto error_release_sock;
  568. default:
  569. break;
  570. }
  571. ret = mutex_lock_interruptible(&call->user_mutex);
  572. release_sock(&rx->sk);
  573. if (ret < 0) {
  574. ret = -ERESTARTSYS;
  575. goto error_put;
  576. }
  577. if (p.call.tx_total_len != -1) {
  578. ret = -EINVAL;
  579. if (call->tx_total_len != -1 ||
  580. call->tx_pending ||
  581. call->tx_top != 0)
  582. goto error_put;
  583. call->tx_total_len = p.call.tx_total_len;
  584. }
  585. }
  586. switch (p.call.nr_timeouts) {
  587. case 3:
  588. j = msecs_to_jiffies(p.call.timeouts.normal);
  589. if (p.call.timeouts.normal > 0 && j == 0)
  590. j = 1;
  591. WRITE_ONCE(call->next_rx_timo, j);
  592. /* Fall through */
  593. case 2:
  594. j = msecs_to_jiffies(p.call.timeouts.idle);
  595. if (p.call.timeouts.idle > 0 && j == 0)
  596. j = 1;
  597. WRITE_ONCE(call->next_req_timo, j);
  598. /* Fall through */
  599. case 1:
  600. if (p.call.timeouts.hard > 0) {
  601. j = msecs_to_jiffies(p.call.timeouts.hard);
  602. now = jiffies;
  603. j += now;
  604. WRITE_ONCE(call->expect_term_by, j);
  605. rxrpc_reduce_call_timer(call, j, now,
  606. rxrpc_timer_set_for_hard);
  607. }
  608. break;
  609. }
  610. state = READ_ONCE(call->state);
  611. _debug("CALL %d USR %lx ST %d on CONN %p",
  612. call->debug_id, call->user_call_ID, state, call->conn);
  613. if (state >= RXRPC_CALL_COMPLETE) {
  614. /* it's too late for this call */
  615. ret = -ESHUTDOWN;
  616. } else if (p.command == RXRPC_CMD_SEND_ABORT) {
  617. ret = 0;
  618. if (rxrpc_abort_call("CMD", call, 0, p.abort_code, -ECONNABORTED))
  619. ret = rxrpc_send_abort_packet(call);
  620. } else if (p.command != RXRPC_CMD_SEND_DATA) {
  621. ret = -EINVAL;
  622. } else if (rxrpc_is_client_call(call) &&
  623. state != RXRPC_CALL_CLIENT_SEND_REQUEST) {
  624. /* request phase complete for this client call */
  625. ret = -EPROTO;
  626. } else if (rxrpc_is_service_call(call) &&
  627. state != RXRPC_CALL_SERVER_ACK_REQUEST &&
  628. state != RXRPC_CALL_SERVER_SEND_REPLY) {
  629. /* Reply phase not begun or not complete for service call. */
  630. ret = -EPROTO;
  631. } else {
  632. ret = rxrpc_send_data(rx, call, msg, len, NULL);
  633. }
  634. out_put_unlock:
  635. mutex_unlock(&call->user_mutex);
  636. error_put:
  637. rxrpc_put_call(call, rxrpc_call_put);
  638. _leave(" = %d", ret);
  639. return ret;
  640. error_release_sock:
  641. release_sock(&rx->sk);
  642. return ret;
  643. }
  644. /**
  645. * rxrpc_kernel_send_data - Allow a kernel service to send data on a call
  646. * @sock: The socket the call is on
  647. * @call: The call to send data through
  648. * @msg: The data to send
  649. * @len: The amount of data to send
  650. * @notify_end_tx: Notification that the last packet is queued.
  651. *
  652. * Allow a kernel service to send data on a call. The call must be in an state
  653. * appropriate to sending data. No control data should be supplied in @msg,
  654. * nor should an address be supplied. MSG_MORE should be flagged if there's
  655. * more data to come, otherwise this data will end the transmission phase.
  656. */
  657. int rxrpc_kernel_send_data(struct socket *sock, struct rxrpc_call *call,
  658. struct msghdr *msg, size_t len,
  659. rxrpc_notify_end_tx_t notify_end_tx)
  660. {
  661. int ret;
  662. _enter("{%d,%s},", call->debug_id, rxrpc_call_states[call->state]);
  663. ASSERTCMP(msg->msg_name, ==, NULL);
  664. ASSERTCMP(msg->msg_control, ==, NULL);
  665. mutex_lock(&call->user_mutex);
  666. _debug("CALL %d USR %lx ST %d on CONN %p",
  667. call->debug_id, call->user_call_ID, call->state, call->conn);
  668. switch (READ_ONCE(call->state)) {
  669. case RXRPC_CALL_CLIENT_SEND_REQUEST:
  670. case RXRPC_CALL_SERVER_ACK_REQUEST:
  671. case RXRPC_CALL_SERVER_SEND_REPLY:
  672. ret = rxrpc_send_data(rxrpc_sk(sock->sk), call, msg, len,
  673. notify_end_tx);
  674. break;
  675. case RXRPC_CALL_COMPLETE:
  676. read_lock_bh(&call->state_lock);
  677. ret = call->error;
  678. read_unlock_bh(&call->state_lock);
  679. break;
  680. default:
  681. /* Request phase complete for this client call */
  682. trace_rxrpc_rx_eproto(call, 0, tracepoint_string("late_send"));
  683. ret = -EPROTO;
  684. break;
  685. }
  686. mutex_unlock(&call->user_mutex);
  687. _leave(" = %d", ret);
  688. return ret;
  689. }
  690. EXPORT_SYMBOL(rxrpc_kernel_send_data);
  691. /**
  692. * rxrpc_kernel_abort_call - Allow a kernel service to abort a call
  693. * @sock: The socket the call is on
  694. * @call: The call to be aborted
  695. * @abort_code: The abort code to stick into the ABORT packet
  696. * @error: Local error value
  697. * @why: 3-char string indicating why.
  698. *
  699. * Allow a kernel service to abort a call, if it's still in an abortable state
  700. * and return true if the call was aborted, false if it was already complete.
  701. */
  702. bool rxrpc_kernel_abort_call(struct socket *sock, struct rxrpc_call *call,
  703. u32 abort_code, int error, const char *why)
  704. {
  705. bool aborted;
  706. _enter("{%d},%d,%d,%s", call->debug_id, abort_code, error, why);
  707. mutex_lock(&call->user_mutex);
  708. aborted = rxrpc_abort_call(why, call, 0, abort_code, error);
  709. if (aborted)
  710. rxrpc_send_abort_packet(call);
  711. mutex_unlock(&call->user_mutex);
  712. return aborted;
  713. }
  714. EXPORT_SYMBOL(rxrpc_kernel_abort_call);
  715. /**
  716. * rxrpc_kernel_set_tx_length - Set the total Tx length on a call
  717. * @sock: The socket the call is on
  718. * @call: The call to be informed
  719. * @tx_total_len: The amount of data to be transmitted for this call
  720. *
  721. * Allow a kernel service to set the total transmit length on a call. This
  722. * allows buffer-to-packet encrypt-and-copy to be performed.
  723. *
  724. * This function is primarily for use for setting the reply length since the
  725. * request length can be set when beginning the call.
  726. */
  727. void rxrpc_kernel_set_tx_length(struct socket *sock, struct rxrpc_call *call,
  728. s64 tx_total_len)
  729. {
  730. WARN_ON(call->tx_total_len != -1);
  731. call->tx_total_len = tx_total_len;
  732. }
  733. EXPORT_SYMBOL(rxrpc_kernel_set_tx_length);