sendmsg.c 15 KB

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