sendmsg.c 15 KB

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