sendmsg.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613
  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 <net/sock.h>
  17. #include <net/af_rxrpc.h>
  18. #include "ar-internal.h"
  19. enum rxrpc_command {
  20. RXRPC_CMD_SEND_DATA, /* send data message */
  21. RXRPC_CMD_SEND_ABORT, /* request abort generation */
  22. RXRPC_CMD_ACCEPT, /* [server] accept incoming call */
  23. RXRPC_CMD_REJECT_BUSY, /* [server] reject a call as busy */
  24. };
  25. /*
  26. * wait for space to appear in the transmit/ACK window
  27. * - caller holds the socket locked
  28. */
  29. static int rxrpc_wait_for_tx_window(struct rxrpc_sock *rx,
  30. struct rxrpc_call *call,
  31. long *timeo)
  32. {
  33. DECLARE_WAITQUEUE(myself, current);
  34. int ret;
  35. _enter(",{%u,%u,%u}",
  36. call->tx_hard_ack, call->tx_top, call->tx_winsize);
  37. add_wait_queue(&call->waitq, &myself);
  38. for (;;) {
  39. set_current_state(TASK_INTERRUPTIBLE);
  40. ret = 0;
  41. if (call->tx_top - call->tx_hard_ack <
  42. min_t(unsigned int, call->tx_winsize,
  43. call->cong_cwnd + call->cong_extra))
  44. break;
  45. if (call->state >= RXRPC_CALL_COMPLETE) {
  46. ret = -call->error;
  47. break;
  48. }
  49. if (signal_pending(current)) {
  50. ret = sock_intr_errno(*timeo);
  51. break;
  52. }
  53. trace_rxrpc_transmit(call, rxrpc_transmit_wait);
  54. release_sock(&rx->sk);
  55. *timeo = schedule_timeout(*timeo);
  56. lock_sock(&rx->sk);
  57. }
  58. remove_wait_queue(&call->waitq, &myself);
  59. set_current_state(TASK_RUNNING);
  60. _leave(" = %d", ret);
  61. return ret;
  62. }
  63. /*
  64. * Schedule an instant Tx resend.
  65. */
  66. static inline void rxrpc_instant_resend(struct rxrpc_call *call, int ix)
  67. {
  68. spin_lock_bh(&call->lock);
  69. if (call->state < RXRPC_CALL_COMPLETE) {
  70. call->rxtx_annotations[ix] =
  71. (call->rxtx_annotations[ix] & RXRPC_TX_ANNO_LAST) |
  72. RXRPC_TX_ANNO_RETRANS;
  73. if (!test_and_set_bit(RXRPC_CALL_EV_RESEND, &call->events))
  74. rxrpc_queue_call(call);
  75. }
  76. spin_unlock_bh(&call->lock);
  77. }
  78. /*
  79. * Queue a DATA packet for transmission, set the resend timeout and send the
  80. * packet immediately
  81. */
  82. static void rxrpc_queue_packet(struct rxrpc_call *call, struct sk_buff *skb,
  83. bool last)
  84. {
  85. struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
  86. rxrpc_seq_t seq = sp->hdr.seq;
  87. int ret, ix;
  88. u8 annotation = RXRPC_TX_ANNO_UNACK;
  89. _net("queue skb %p [%d]", skb, seq);
  90. ASSERTCMP(seq, ==, call->tx_top + 1);
  91. if (last)
  92. annotation |= RXRPC_TX_ANNO_LAST;
  93. /* We have to set the timestamp before queueing as the retransmit
  94. * algorithm can see the packet as soon as we queue it.
  95. */
  96. skb->tstamp = ktime_get_real();
  97. ix = seq & RXRPC_RXTX_BUFF_MASK;
  98. rxrpc_get_skb(skb, rxrpc_skb_tx_got);
  99. call->rxtx_annotations[ix] = annotation;
  100. smp_wmb();
  101. call->rxtx_buffer[ix] = skb;
  102. call->tx_top = seq;
  103. if (last)
  104. trace_rxrpc_transmit(call, rxrpc_transmit_queue_last);
  105. else
  106. trace_rxrpc_transmit(call, rxrpc_transmit_queue);
  107. if (last || call->state == RXRPC_CALL_SERVER_ACK_REQUEST) {
  108. _debug("________awaiting reply/ACK__________");
  109. write_lock_bh(&call->state_lock);
  110. switch (call->state) {
  111. case RXRPC_CALL_CLIENT_SEND_REQUEST:
  112. call->state = RXRPC_CALL_CLIENT_AWAIT_REPLY;
  113. break;
  114. case RXRPC_CALL_SERVER_ACK_REQUEST:
  115. call->state = RXRPC_CALL_SERVER_SEND_REPLY;
  116. call->ack_at = call->expire_at;
  117. if (call->ackr_reason == RXRPC_ACK_DELAY)
  118. call->ackr_reason = 0;
  119. __rxrpc_set_timer(call, rxrpc_timer_init_for_send_reply,
  120. ktime_get_real());
  121. if (!last)
  122. break;
  123. case RXRPC_CALL_SERVER_SEND_REPLY:
  124. call->state = RXRPC_CALL_SERVER_AWAIT_ACK;
  125. break;
  126. default:
  127. break;
  128. }
  129. write_unlock_bh(&call->state_lock);
  130. }
  131. if (seq == 1 && rxrpc_is_client_call(call))
  132. rxrpc_expose_client_call(call);
  133. ret = rxrpc_send_data_packet(call, skb, false);
  134. if (ret < 0) {
  135. _debug("need instant resend %d", ret);
  136. rxrpc_instant_resend(call, ix);
  137. } else {
  138. ktime_t now = ktime_get_real(), resend_at;
  139. resend_at = ktime_add_ms(now, rxrpc_resend_timeout);
  140. if (ktime_before(resend_at, call->resend_at)) {
  141. call->resend_at = resend_at;
  142. rxrpc_set_timer(call, rxrpc_timer_set_for_send, now);
  143. }
  144. }
  145. rxrpc_free_skb(skb, rxrpc_skb_tx_freed);
  146. _leave("");
  147. }
  148. /*
  149. * send data through a socket
  150. * - must be called in process context
  151. * - caller holds the socket locked
  152. */
  153. static int rxrpc_send_data(struct rxrpc_sock *rx,
  154. struct rxrpc_call *call,
  155. struct msghdr *msg, size_t len)
  156. {
  157. struct rxrpc_skb_priv *sp;
  158. struct sk_buff *skb;
  159. struct sock *sk = &rx->sk;
  160. long timeo;
  161. bool more;
  162. int ret, copied;
  163. timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
  164. /* this should be in poll */
  165. sk_clear_bit(SOCKWQ_ASYNC_NOSPACE, sk);
  166. if (sk->sk_err || (sk->sk_shutdown & SEND_SHUTDOWN))
  167. return -EPIPE;
  168. more = msg->msg_flags & MSG_MORE;
  169. skb = call->tx_pending;
  170. call->tx_pending = NULL;
  171. rxrpc_see_skb(skb, rxrpc_skb_tx_seen);
  172. copied = 0;
  173. do {
  174. /* Check to see if there's a ping ACK to reply to. */
  175. if (call->ackr_reason == RXRPC_ACK_PING_RESPONSE)
  176. rxrpc_send_ack_packet(call, false);
  177. if (!skb) {
  178. size_t size, chunk, max, space;
  179. _debug("alloc");
  180. if (call->tx_top - call->tx_hard_ack >=
  181. min_t(unsigned int, call->tx_winsize,
  182. call->cong_cwnd + call->cong_extra)) {
  183. ret = -EAGAIN;
  184. if (msg->msg_flags & MSG_DONTWAIT)
  185. goto maybe_error;
  186. ret = rxrpc_wait_for_tx_window(rx, call,
  187. &timeo);
  188. if (ret < 0)
  189. goto maybe_error;
  190. }
  191. max = RXRPC_JUMBO_DATALEN;
  192. max -= call->conn->security_size;
  193. max &= ~(call->conn->size_align - 1UL);
  194. chunk = max;
  195. if (chunk > msg_data_left(msg) && !more)
  196. chunk = msg_data_left(msg);
  197. space = chunk + call->conn->size_align;
  198. space &= ~(call->conn->size_align - 1UL);
  199. size = space + call->conn->security_size;
  200. _debug("SIZE: %zu/%zu/%zu", chunk, space, size);
  201. /* create a buffer that we can retain until it's ACK'd */
  202. skb = sock_alloc_send_skb(
  203. sk, size, msg->msg_flags & MSG_DONTWAIT, &ret);
  204. if (!skb)
  205. goto maybe_error;
  206. rxrpc_new_skb(skb, rxrpc_skb_tx_new);
  207. _debug("ALLOC SEND %p", skb);
  208. ASSERTCMP(skb->mark, ==, 0);
  209. _debug("HS: %u", call->conn->security_size);
  210. skb_reserve(skb, call->conn->security_size);
  211. skb->len += call->conn->security_size;
  212. sp = rxrpc_skb(skb);
  213. sp->remain = chunk;
  214. if (sp->remain > skb_tailroom(skb))
  215. sp->remain = skb_tailroom(skb);
  216. _net("skb: hr %d, tr %d, hl %d, rm %d",
  217. skb_headroom(skb),
  218. skb_tailroom(skb),
  219. skb_headlen(skb),
  220. sp->remain);
  221. skb->ip_summed = CHECKSUM_UNNECESSARY;
  222. }
  223. _debug("append");
  224. sp = rxrpc_skb(skb);
  225. /* append next segment of data to the current buffer */
  226. if (msg_data_left(msg) > 0) {
  227. int copy = skb_tailroom(skb);
  228. ASSERTCMP(copy, >, 0);
  229. if (copy > msg_data_left(msg))
  230. copy = msg_data_left(msg);
  231. if (copy > sp->remain)
  232. copy = sp->remain;
  233. _debug("add");
  234. ret = skb_add_data(skb, &msg->msg_iter, copy);
  235. _debug("added");
  236. if (ret < 0)
  237. goto efault;
  238. sp->remain -= copy;
  239. skb->mark += copy;
  240. copied += copy;
  241. }
  242. /* check for the far side aborting the call or a network error
  243. * occurring */
  244. if (call->state == RXRPC_CALL_COMPLETE)
  245. goto call_terminated;
  246. /* add the packet to the send queue if it's now full */
  247. if (sp->remain <= 0 ||
  248. (msg_data_left(msg) == 0 && !more)) {
  249. struct rxrpc_connection *conn = call->conn;
  250. uint32_t seq;
  251. size_t pad;
  252. /* pad out if we're using security */
  253. if (conn->security_ix) {
  254. pad = conn->security_size + skb->mark;
  255. pad = conn->size_align - pad;
  256. pad &= conn->size_align - 1;
  257. _debug("pad %zu", pad);
  258. if (pad)
  259. memset(skb_put(skb, pad), 0, pad);
  260. }
  261. seq = call->tx_top + 1;
  262. sp->hdr.seq = seq;
  263. sp->hdr._rsvd = 0;
  264. sp->hdr.flags = conn->out_clientflag;
  265. if (msg_data_left(msg) == 0 && !more)
  266. sp->hdr.flags |= RXRPC_LAST_PACKET;
  267. else if (call->tx_top - call->tx_hard_ack <
  268. call->tx_winsize)
  269. sp->hdr.flags |= RXRPC_MORE_PACKETS;
  270. ret = conn->security->secure_packet(
  271. call, skb, skb->mark, skb->head);
  272. if (ret < 0)
  273. goto out;
  274. rxrpc_queue_packet(call, skb, !msg_data_left(msg) && !more);
  275. skb = NULL;
  276. }
  277. } while (msg_data_left(msg) > 0);
  278. success:
  279. ret = copied;
  280. out:
  281. call->tx_pending = skb;
  282. _leave(" = %d", ret);
  283. return ret;
  284. call_terminated:
  285. rxrpc_free_skb(skb, rxrpc_skb_tx_freed);
  286. _leave(" = %d", -call->error);
  287. return -call->error;
  288. maybe_error:
  289. if (copied)
  290. goto success;
  291. goto out;
  292. efault:
  293. ret = -EFAULT;
  294. goto out;
  295. }
  296. /*
  297. * extract control messages from the sendmsg() control buffer
  298. */
  299. static int rxrpc_sendmsg_cmsg(struct msghdr *msg,
  300. unsigned long *user_call_ID,
  301. enum rxrpc_command *command,
  302. u32 *abort_code,
  303. bool *_exclusive)
  304. {
  305. struct cmsghdr *cmsg;
  306. bool got_user_ID = false;
  307. int len;
  308. *command = RXRPC_CMD_SEND_DATA;
  309. if (msg->msg_controllen == 0)
  310. return -EINVAL;
  311. for_each_cmsghdr(cmsg, msg) {
  312. if (!CMSG_OK(msg, cmsg))
  313. return -EINVAL;
  314. len = cmsg->cmsg_len - CMSG_ALIGN(sizeof(struct cmsghdr));
  315. _debug("CMSG %d, %d, %d",
  316. cmsg->cmsg_level, cmsg->cmsg_type, len);
  317. if (cmsg->cmsg_level != SOL_RXRPC)
  318. continue;
  319. switch (cmsg->cmsg_type) {
  320. case RXRPC_USER_CALL_ID:
  321. if (msg->msg_flags & MSG_CMSG_COMPAT) {
  322. if (len != sizeof(u32))
  323. return -EINVAL;
  324. *user_call_ID = *(u32 *) CMSG_DATA(cmsg);
  325. } else {
  326. if (len != sizeof(unsigned long))
  327. return -EINVAL;
  328. *user_call_ID = *(unsigned long *)
  329. CMSG_DATA(cmsg);
  330. }
  331. _debug("User Call ID %lx", *user_call_ID);
  332. got_user_ID = true;
  333. break;
  334. case RXRPC_ABORT:
  335. if (*command != RXRPC_CMD_SEND_DATA)
  336. return -EINVAL;
  337. *command = RXRPC_CMD_SEND_ABORT;
  338. if (len != sizeof(*abort_code))
  339. return -EINVAL;
  340. *abort_code = *(unsigned int *) CMSG_DATA(cmsg);
  341. _debug("Abort %x", *abort_code);
  342. if (*abort_code == 0)
  343. return -EINVAL;
  344. break;
  345. case RXRPC_ACCEPT:
  346. if (*command != RXRPC_CMD_SEND_DATA)
  347. return -EINVAL;
  348. *command = RXRPC_CMD_ACCEPT;
  349. if (len != 0)
  350. return -EINVAL;
  351. break;
  352. case RXRPC_EXCLUSIVE_CALL:
  353. *_exclusive = true;
  354. if (len != 0)
  355. return -EINVAL;
  356. break;
  357. default:
  358. return -EINVAL;
  359. }
  360. }
  361. if (!got_user_ID)
  362. return -EINVAL;
  363. _leave(" = 0");
  364. return 0;
  365. }
  366. /*
  367. * Create a new client call for sendmsg().
  368. */
  369. static struct rxrpc_call *
  370. rxrpc_new_client_call_for_sendmsg(struct rxrpc_sock *rx, struct msghdr *msg,
  371. unsigned long user_call_ID, bool exclusive)
  372. {
  373. struct rxrpc_conn_parameters cp;
  374. struct rxrpc_call *call;
  375. struct key *key;
  376. DECLARE_SOCKADDR(struct sockaddr_rxrpc *, srx, msg->msg_name);
  377. _enter("");
  378. if (!msg->msg_name)
  379. return ERR_PTR(-EDESTADDRREQ);
  380. key = rx->key;
  381. if (key && !rx->key->payload.data[0])
  382. key = NULL;
  383. memset(&cp, 0, sizeof(cp));
  384. cp.local = rx->local;
  385. cp.key = rx->key;
  386. cp.security_level = rx->min_sec_level;
  387. cp.exclusive = rx->exclusive | exclusive;
  388. cp.service_id = srx->srx_service;
  389. call = rxrpc_new_client_call(rx, &cp, srx, user_call_ID, GFP_KERNEL);
  390. _leave(" = %p\n", call);
  391. return call;
  392. }
  393. /*
  394. * send a message forming part of a client call through an RxRPC socket
  395. * - caller holds the socket locked
  396. * - the socket may be either a client socket or a server socket
  397. */
  398. int rxrpc_do_sendmsg(struct rxrpc_sock *rx, struct msghdr *msg, size_t len)
  399. {
  400. enum rxrpc_command cmd;
  401. struct rxrpc_call *call;
  402. unsigned long user_call_ID = 0;
  403. bool exclusive = false;
  404. u32 abort_code = 0;
  405. int ret;
  406. _enter("");
  407. ret = rxrpc_sendmsg_cmsg(msg, &user_call_ID, &cmd, &abort_code,
  408. &exclusive);
  409. if (ret < 0)
  410. return ret;
  411. if (cmd == RXRPC_CMD_ACCEPT) {
  412. if (rx->sk.sk_state != RXRPC_SERVER_LISTENING)
  413. return -EINVAL;
  414. call = rxrpc_accept_call(rx, user_call_ID, NULL);
  415. if (IS_ERR(call))
  416. return PTR_ERR(call);
  417. rxrpc_put_call(call, rxrpc_call_put);
  418. return 0;
  419. }
  420. call = rxrpc_find_call_by_user_ID(rx, user_call_ID);
  421. if (!call) {
  422. if (cmd != RXRPC_CMD_SEND_DATA)
  423. return -EBADSLT;
  424. call = rxrpc_new_client_call_for_sendmsg(rx, msg, user_call_ID,
  425. exclusive);
  426. if (IS_ERR(call))
  427. return PTR_ERR(call);
  428. }
  429. _debug("CALL %d USR %lx ST %d on CONN %p",
  430. call->debug_id, call->user_call_ID, call->state, call->conn);
  431. if (call->state >= RXRPC_CALL_COMPLETE) {
  432. /* it's too late for this call */
  433. ret = -ESHUTDOWN;
  434. } else if (cmd == RXRPC_CMD_SEND_ABORT) {
  435. ret = 0;
  436. if (rxrpc_abort_call("CMD", call, 0, abort_code, ECONNABORTED))
  437. ret = rxrpc_send_abort_packet(call);
  438. } else if (cmd != RXRPC_CMD_SEND_DATA) {
  439. ret = -EINVAL;
  440. } else if (rxrpc_is_client_call(call) &&
  441. call->state != RXRPC_CALL_CLIENT_SEND_REQUEST) {
  442. /* request phase complete for this client call */
  443. ret = -EPROTO;
  444. } else if (rxrpc_is_service_call(call) &&
  445. call->state != RXRPC_CALL_SERVER_ACK_REQUEST &&
  446. call->state != RXRPC_CALL_SERVER_SEND_REPLY) {
  447. /* Reply phase not begun or not complete for service call. */
  448. ret = -EPROTO;
  449. } else {
  450. ret = rxrpc_send_data(rx, call, msg, len);
  451. }
  452. rxrpc_put_call(call, rxrpc_call_put);
  453. _leave(" = %d", ret);
  454. return ret;
  455. }
  456. /**
  457. * rxrpc_kernel_send_data - Allow a kernel service to send data on a call
  458. * @sock: The socket the call is on
  459. * @call: The call to send data through
  460. * @msg: The data to send
  461. * @len: The amount of data to send
  462. *
  463. * Allow a kernel service to send data on a call. The call must be in an state
  464. * appropriate to sending data. No control data should be supplied in @msg,
  465. * nor should an address be supplied. MSG_MORE should be flagged if there's
  466. * more data to come, otherwise this data will end the transmission phase.
  467. */
  468. int rxrpc_kernel_send_data(struct socket *sock, struct rxrpc_call *call,
  469. struct msghdr *msg, size_t len)
  470. {
  471. int ret;
  472. _enter("{%d,%s},", call->debug_id, rxrpc_call_states[call->state]);
  473. ASSERTCMP(msg->msg_name, ==, NULL);
  474. ASSERTCMP(msg->msg_control, ==, NULL);
  475. lock_sock(sock->sk);
  476. _debug("CALL %d USR %lx ST %d on CONN %p",
  477. call->debug_id, call->user_call_ID, call->state, call->conn);
  478. if (call->state >= RXRPC_CALL_COMPLETE) {
  479. ret = -ESHUTDOWN; /* it's too late for this call */
  480. } else if (call->state != RXRPC_CALL_CLIENT_SEND_REQUEST &&
  481. call->state != RXRPC_CALL_SERVER_ACK_REQUEST &&
  482. call->state != RXRPC_CALL_SERVER_SEND_REPLY) {
  483. ret = -EPROTO; /* request phase complete for this client call */
  484. } else {
  485. ret = rxrpc_send_data(rxrpc_sk(sock->sk), call, msg, len);
  486. }
  487. release_sock(sock->sk);
  488. _leave(" = %d", ret);
  489. return ret;
  490. }
  491. EXPORT_SYMBOL(rxrpc_kernel_send_data);
  492. /**
  493. * rxrpc_kernel_abort_call - Allow a kernel service to abort a call
  494. * @sock: The socket the call is on
  495. * @call: The call to be aborted
  496. * @abort_code: The abort code to stick into the ABORT packet
  497. * @error: Local error value
  498. * @why: 3-char string indicating why.
  499. *
  500. * Allow a kernel service to abort a call, if it's still in an abortable state.
  501. */
  502. void rxrpc_kernel_abort_call(struct socket *sock, struct rxrpc_call *call,
  503. u32 abort_code, int error, const char *why)
  504. {
  505. _enter("{%d},%d,%d,%s", call->debug_id, abort_code, error, why);
  506. lock_sock(sock->sk);
  507. if (rxrpc_abort_call(why, call, 0, abort_code, error))
  508. rxrpc_send_abort_packet(call);
  509. release_sock(sock->sk);
  510. _leave("");
  511. }
  512. EXPORT_SYMBOL(rxrpc_kernel_abort_call);