ar-output.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718
  1. /* RxRPC packet transmission
  2. *
  3. * Copyright (C) 2007 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 License
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the License, or (at your option) any later version.
  10. */
  11. #include <linux/net.h>
  12. #include <linux/gfp.h>
  13. #include <linux/skbuff.h>
  14. #include <linux/circ_buf.h>
  15. #include <linux/export.h>
  16. #include <net/sock.h>
  17. #include <net/af_rxrpc.h>
  18. #include "ar-internal.h"
  19. /*
  20. * Time till packet resend (in jiffies).
  21. */
  22. unsigned rxrpc_resend_timeout = 4 * HZ;
  23. static int rxrpc_send_data(struct kiocb *iocb,
  24. struct rxrpc_sock *rx,
  25. struct rxrpc_call *call,
  26. struct msghdr *msg, size_t len);
  27. /*
  28. * extract control messages from the sendmsg() control buffer
  29. */
  30. static int rxrpc_sendmsg_cmsg(struct rxrpc_sock *rx, struct msghdr *msg,
  31. unsigned long *user_call_ID,
  32. enum rxrpc_command *command,
  33. u32 *abort_code,
  34. bool server)
  35. {
  36. struct cmsghdr *cmsg;
  37. int len;
  38. *command = RXRPC_CMD_SEND_DATA;
  39. if (msg->msg_controllen == 0)
  40. return -EINVAL;
  41. for_each_cmsghdr(cmsg, msg) {
  42. if (!CMSG_OK(msg, cmsg))
  43. return -EINVAL;
  44. len = cmsg->cmsg_len - CMSG_ALIGN(sizeof(struct cmsghdr));
  45. _debug("CMSG %d, %d, %d",
  46. cmsg->cmsg_level, cmsg->cmsg_type, len);
  47. if (cmsg->cmsg_level != SOL_RXRPC)
  48. continue;
  49. switch (cmsg->cmsg_type) {
  50. case RXRPC_USER_CALL_ID:
  51. if (msg->msg_flags & MSG_CMSG_COMPAT) {
  52. if (len != sizeof(u32))
  53. return -EINVAL;
  54. *user_call_ID = *(u32 *) CMSG_DATA(cmsg);
  55. } else {
  56. if (len != sizeof(unsigned long))
  57. return -EINVAL;
  58. *user_call_ID = *(unsigned long *)
  59. CMSG_DATA(cmsg);
  60. }
  61. _debug("User Call ID %lx", *user_call_ID);
  62. break;
  63. case RXRPC_ABORT:
  64. if (*command != RXRPC_CMD_SEND_DATA)
  65. return -EINVAL;
  66. *command = RXRPC_CMD_SEND_ABORT;
  67. if (len != sizeof(*abort_code))
  68. return -EINVAL;
  69. *abort_code = *(unsigned int *) CMSG_DATA(cmsg);
  70. _debug("Abort %x", *abort_code);
  71. if (*abort_code == 0)
  72. return -EINVAL;
  73. break;
  74. case RXRPC_ACCEPT:
  75. if (*command != RXRPC_CMD_SEND_DATA)
  76. return -EINVAL;
  77. *command = RXRPC_CMD_ACCEPT;
  78. if (len != 0)
  79. return -EINVAL;
  80. if (!server)
  81. return -EISCONN;
  82. break;
  83. default:
  84. return -EINVAL;
  85. }
  86. }
  87. _leave(" = 0");
  88. return 0;
  89. }
  90. /*
  91. * abort a call, sending an ABORT packet to the peer
  92. */
  93. static void rxrpc_send_abort(struct rxrpc_call *call, u32 abort_code)
  94. {
  95. write_lock_bh(&call->state_lock);
  96. if (call->state <= RXRPC_CALL_COMPLETE) {
  97. call->state = RXRPC_CALL_LOCALLY_ABORTED;
  98. call->abort_code = abort_code;
  99. set_bit(RXRPC_CALL_ABORT, &call->events);
  100. del_timer_sync(&call->resend_timer);
  101. del_timer_sync(&call->ack_timer);
  102. clear_bit(RXRPC_CALL_RESEND_TIMER, &call->events);
  103. clear_bit(RXRPC_CALL_ACK, &call->events);
  104. clear_bit(RXRPC_CALL_RUN_RTIMER, &call->flags);
  105. rxrpc_queue_call(call);
  106. }
  107. write_unlock_bh(&call->state_lock);
  108. }
  109. /*
  110. * send a message forming part of a client call through an RxRPC socket
  111. * - caller holds the socket locked
  112. * - the socket may be either a client socket or a server socket
  113. */
  114. int rxrpc_client_sendmsg(struct kiocb *iocb, struct rxrpc_sock *rx,
  115. struct rxrpc_transport *trans, struct msghdr *msg,
  116. size_t len)
  117. {
  118. struct rxrpc_conn_bundle *bundle;
  119. enum rxrpc_command cmd;
  120. struct rxrpc_call *call;
  121. unsigned long user_call_ID = 0;
  122. struct key *key;
  123. __be16 service_id;
  124. u32 abort_code = 0;
  125. int ret;
  126. _enter("");
  127. ASSERT(trans != NULL);
  128. ret = rxrpc_sendmsg_cmsg(rx, msg, &user_call_ID, &cmd, &abort_code,
  129. false);
  130. if (ret < 0)
  131. return ret;
  132. bundle = NULL;
  133. if (trans) {
  134. service_id = rx->service_id;
  135. if (msg->msg_name) {
  136. DECLARE_SOCKADDR(struct sockaddr_rxrpc *, srx,
  137. msg->msg_name);
  138. service_id = htons(srx->srx_service);
  139. }
  140. key = rx->key;
  141. if (key && !rx->key->payload.data)
  142. key = NULL;
  143. bundle = rxrpc_get_bundle(rx, trans, key, service_id,
  144. GFP_KERNEL);
  145. if (IS_ERR(bundle))
  146. return PTR_ERR(bundle);
  147. }
  148. call = rxrpc_get_client_call(rx, trans, bundle, user_call_ID,
  149. abort_code == 0, GFP_KERNEL);
  150. if (trans)
  151. rxrpc_put_bundle(trans, bundle);
  152. if (IS_ERR(call)) {
  153. _leave(" = %ld", PTR_ERR(call));
  154. return PTR_ERR(call);
  155. }
  156. _debug("CALL %d USR %lx ST %d on CONN %p",
  157. call->debug_id, call->user_call_ID, call->state, call->conn);
  158. if (call->state >= RXRPC_CALL_COMPLETE) {
  159. /* it's too late for this call */
  160. ret = -ESHUTDOWN;
  161. } else if (cmd == RXRPC_CMD_SEND_ABORT) {
  162. rxrpc_send_abort(call, abort_code);
  163. } else if (cmd != RXRPC_CMD_SEND_DATA) {
  164. ret = -EINVAL;
  165. } else if (call->state != RXRPC_CALL_CLIENT_SEND_REQUEST) {
  166. /* request phase complete for this client call */
  167. ret = -EPROTO;
  168. } else {
  169. ret = rxrpc_send_data(iocb, rx, call, msg, len);
  170. }
  171. rxrpc_put_call(call);
  172. _leave(" = %d", ret);
  173. return ret;
  174. }
  175. /**
  176. * rxrpc_kernel_send_data - Allow a kernel service to send data on a call
  177. * @call: The call to send data through
  178. * @msg: The data to send
  179. * @len: The amount of data to send
  180. *
  181. * Allow a kernel service to send data on a call. The call must be in an state
  182. * appropriate to sending data. No control data should be supplied in @msg,
  183. * nor should an address be supplied. MSG_MORE should be flagged if there's
  184. * more data to come, otherwise this data will end the transmission phase.
  185. */
  186. int rxrpc_kernel_send_data(struct rxrpc_call *call, struct msghdr *msg,
  187. size_t len)
  188. {
  189. int ret;
  190. _enter("{%d,%s},", call->debug_id, rxrpc_call_states[call->state]);
  191. ASSERTCMP(msg->msg_name, ==, NULL);
  192. ASSERTCMP(msg->msg_control, ==, NULL);
  193. lock_sock(&call->socket->sk);
  194. _debug("CALL %d USR %lx ST %d on CONN %p",
  195. call->debug_id, call->user_call_ID, call->state, call->conn);
  196. if (call->state >= RXRPC_CALL_COMPLETE) {
  197. ret = -ESHUTDOWN; /* it's too late for this call */
  198. } else if (call->state != RXRPC_CALL_CLIENT_SEND_REQUEST &&
  199. call->state != RXRPC_CALL_SERVER_ACK_REQUEST &&
  200. call->state != RXRPC_CALL_SERVER_SEND_REPLY) {
  201. ret = -EPROTO; /* request phase complete for this client call */
  202. } else {
  203. ret = rxrpc_send_data(NULL, call->socket, call, msg, len);
  204. }
  205. release_sock(&call->socket->sk);
  206. _leave(" = %d", ret);
  207. return ret;
  208. }
  209. EXPORT_SYMBOL(rxrpc_kernel_send_data);
  210. /**
  211. * rxrpc_kernel_abort_call - Allow a kernel service to abort a call
  212. * @call: The call to be aborted
  213. * @abort_code: The abort code to stick into the ABORT packet
  214. *
  215. * Allow a kernel service to abort a call, if it's still in an abortable state.
  216. */
  217. void rxrpc_kernel_abort_call(struct rxrpc_call *call, u32 abort_code)
  218. {
  219. _enter("{%d},%d", call->debug_id, abort_code);
  220. lock_sock(&call->socket->sk);
  221. _debug("CALL %d USR %lx ST %d on CONN %p",
  222. call->debug_id, call->user_call_ID, call->state, call->conn);
  223. if (call->state < RXRPC_CALL_COMPLETE)
  224. rxrpc_send_abort(call, abort_code);
  225. release_sock(&call->socket->sk);
  226. _leave("");
  227. }
  228. EXPORT_SYMBOL(rxrpc_kernel_abort_call);
  229. /*
  230. * send a message through a server socket
  231. * - caller holds the socket locked
  232. */
  233. int rxrpc_server_sendmsg(struct kiocb *iocb, struct rxrpc_sock *rx,
  234. struct msghdr *msg, size_t len)
  235. {
  236. enum rxrpc_command cmd;
  237. struct rxrpc_call *call;
  238. unsigned long user_call_ID = 0;
  239. u32 abort_code = 0;
  240. int ret;
  241. _enter("");
  242. ret = rxrpc_sendmsg_cmsg(rx, msg, &user_call_ID, &cmd, &abort_code,
  243. true);
  244. if (ret < 0)
  245. return ret;
  246. if (cmd == RXRPC_CMD_ACCEPT) {
  247. call = rxrpc_accept_call(rx, user_call_ID);
  248. if (IS_ERR(call))
  249. return PTR_ERR(call);
  250. rxrpc_put_call(call);
  251. return 0;
  252. }
  253. call = rxrpc_find_server_call(rx, user_call_ID);
  254. if (!call)
  255. return -EBADSLT;
  256. if (call->state >= RXRPC_CALL_COMPLETE) {
  257. ret = -ESHUTDOWN;
  258. goto out;
  259. }
  260. switch (cmd) {
  261. case RXRPC_CMD_SEND_DATA:
  262. if (call->state != RXRPC_CALL_CLIENT_SEND_REQUEST &&
  263. call->state != RXRPC_CALL_SERVER_ACK_REQUEST &&
  264. call->state != RXRPC_CALL_SERVER_SEND_REPLY) {
  265. /* Tx phase not yet begun for this call */
  266. ret = -EPROTO;
  267. break;
  268. }
  269. ret = rxrpc_send_data(iocb, rx, call, msg, len);
  270. break;
  271. case RXRPC_CMD_SEND_ABORT:
  272. rxrpc_send_abort(call, abort_code);
  273. break;
  274. default:
  275. BUG();
  276. }
  277. out:
  278. rxrpc_put_call(call);
  279. _leave(" = %d", ret);
  280. return ret;
  281. }
  282. /*
  283. * send a packet through the transport endpoint
  284. */
  285. int rxrpc_send_packet(struct rxrpc_transport *trans, struct sk_buff *skb)
  286. {
  287. struct kvec iov[1];
  288. struct msghdr msg;
  289. int ret, opt;
  290. _enter(",{%d}", skb->len);
  291. iov[0].iov_base = skb->head;
  292. iov[0].iov_len = skb->len;
  293. msg.msg_name = &trans->peer->srx.transport.sin;
  294. msg.msg_namelen = sizeof(trans->peer->srx.transport.sin);
  295. msg.msg_control = NULL;
  296. msg.msg_controllen = 0;
  297. msg.msg_flags = 0;
  298. /* send the packet with the don't fragment bit set if we currently
  299. * think it's small enough */
  300. if (skb->len - sizeof(struct rxrpc_header) < trans->peer->maxdata) {
  301. down_read(&trans->local->defrag_sem);
  302. /* send the packet by UDP
  303. * - returns -EMSGSIZE if UDP would have to fragment the packet
  304. * to go out of the interface
  305. * - in which case, we'll have processed the ICMP error
  306. * message and update the peer record
  307. */
  308. ret = kernel_sendmsg(trans->local->socket, &msg, iov, 1,
  309. iov[0].iov_len);
  310. up_read(&trans->local->defrag_sem);
  311. if (ret == -EMSGSIZE)
  312. goto send_fragmentable;
  313. _leave(" = %d [%u]", ret, trans->peer->maxdata);
  314. return ret;
  315. }
  316. send_fragmentable:
  317. /* attempt to send this message with fragmentation enabled */
  318. _debug("send fragment");
  319. down_write(&trans->local->defrag_sem);
  320. opt = IP_PMTUDISC_DONT;
  321. ret = kernel_setsockopt(trans->local->socket, SOL_IP, IP_MTU_DISCOVER,
  322. (char *) &opt, sizeof(opt));
  323. if (ret == 0) {
  324. ret = kernel_sendmsg(trans->local->socket, &msg, iov, 1,
  325. iov[0].iov_len);
  326. opt = IP_PMTUDISC_DO;
  327. kernel_setsockopt(trans->local->socket, SOL_IP,
  328. IP_MTU_DISCOVER, (char *) &opt, sizeof(opt));
  329. }
  330. up_write(&trans->local->defrag_sem);
  331. _leave(" = %d [frag %u]", ret, trans->peer->maxdata);
  332. return ret;
  333. }
  334. /*
  335. * wait for space to appear in the transmit/ACK window
  336. * - caller holds the socket locked
  337. */
  338. static int rxrpc_wait_for_tx_window(struct rxrpc_sock *rx,
  339. struct rxrpc_call *call,
  340. long *timeo)
  341. {
  342. DECLARE_WAITQUEUE(myself, current);
  343. int ret;
  344. _enter(",{%d},%ld",
  345. CIRC_SPACE(call->acks_head, call->acks_tail, call->acks_winsz),
  346. *timeo);
  347. add_wait_queue(&call->tx_waitq, &myself);
  348. for (;;) {
  349. set_current_state(TASK_INTERRUPTIBLE);
  350. ret = 0;
  351. if (CIRC_SPACE(call->acks_head, call->acks_tail,
  352. call->acks_winsz) > 0)
  353. break;
  354. if (signal_pending(current)) {
  355. ret = sock_intr_errno(*timeo);
  356. break;
  357. }
  358. release_sock(&rx->sk);
  359. *timeo = schedule_timeout(*timeo);
  360. lock_sock(&rx->sk);
  361. }
  362. remove_wait_queue(&call->tx_waitq, &myself);
  363. set_current_state(TASK_RUNNING);
  364. _leave(" = %d", ret);
  365. return ret;
  366. }
  367. /*
  368. * attempt to schedule an instant Tx resend
  369. */
  370. static inline void rxrpc_instant_resend(struct rxrpc_call *call)
  371. {
  372. read_lock_bh(&call->state_lock);
  373. if (try_to_del_timer_sync(&call->resend_timer) >= 0) {
  374. clear_bit(RXRPC_CALL_RUN_RTIMER, &call->flags);
  375. if (call->state < RXRPC_CALL_COMPLETE &&
  376. !test_and_set_bit(RXRPC_CALL_RESEND_TIMER, &call->events))
  377. rxrpc_queue_call(call);
  378. }
  379. read_unlock_bh(&call->state_lock);
  380. }
  381. /*
  382. * queue a packet for transmission, set the resend timer and attempt
  383. * to send the packet immediately
  384. */
  385. static void rxrpc_queue_packet(struct rxrpc_call *call, struct sk_buff *skb,
  386. bool last)
  387. {
  388. struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
  389. int ret;
  390. _net("queue skb %p [%d]", skb, call->acks_head);
  391. ASSERT(call->acks_window != NULL);
  392. call->acks_window[call->acks_head] = (unsigned long) skb;
  393. smp_wmb();
  394. call->acks_head = (call->acks_head + 1) & (call->acks_winsz - 1);
  395. if (last || call->state == RXRPC_CALL_SERVER_ACK_REQUEST) {
  396. _debug("________awaiting reply/ACK__________");
  397. write_lock_bh(&call->state_lock);
  398. switch (call->state) {
  399. case RXRPC_CALL_CLIENT_SEND_REQUEST:
  400. call->state = RXRPC_CALL_CLIENT_AWAIT_REPLY;
  401. break;
  402. case RXRPC_CALL_SERVER_ACK_REQUEST:
  403. call->state = RXRPC_CALL_SERVER_SEND_REPLY;
  404. if (!last)
  405. break;
  406. case RXRPC_CALL_SERVER_SEND_REPLY:
  407. call->state = RXRPC_CALL_SERVER_AWAIT_ACK;
  408. break;
  409. default:
  410. break;
  411. }
  412. write_unlock_bh(&call->state_lock);
  413. }
  414. _proto("Tx DATA %%%u { #%u }",
  415. ntohl(sp->hdr.serial), ntohl(sp->hdr.seq));
  416. sp->need_resend = false;
  417. sp->resend_at = jiffies + rxrpc_resend_timeout;
  418. if (!test_and_set_bit(RXRPC_CALL_RUN_RTIMER, &call->flags)) {
  419. _debug("run timer");
  420. call->resend_timer.expires = sp->resend_at;
  421. add_timer(&call->resend_timer);
  422. }
  423. /* attempt to cancel the rx-ACK timer, deferring reply transmission if
  424. * we're ACK'ing the request phase of an incoming call */
  425. ret = -EAGAIN;
  426. if (try_to_del_timer_sync(&call->ack_timer) >= 0) {
  427. /* the packet may be freed by rxrpc_process_call() before this
  428. * returns */
  429. ret = rxrpc_send_packet(call->conn->trans, skb);
  430. _net("sent skb %p", skb);
  431. } else {
  432. _debug("failed to delete ACK timer");
  433. }
  434. if (ret < 0) {
  435. _debug("need instant resend %d", ret);
  436. sp->need_resend = true;
  437. rxrpc_instant_resend(call);
  438. }
  439. _leave("");
  440. }
  441. /*
  442. * send data through a socket
  443. * - must be called in process context
  444. * - caller holds the socket locked
  445. */
  446. static int rxrpc_send_data(struct kiocb *iocb,
  447. struct rxrpc_sock *rx,
  448. struct rxrpc_call *call,
  449. struct msghdr *msg, size_t len)
  450. {
  451. struct rxrpc_skb_priv *sp;
  452. struct sk_buff *skb;
  453. struct sock *sk = &rx->sk;
  454. long timeo;
  455. bool more;
  456. int ret, copied;
  457. timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
  458. /* this should be in poll */
  459. clear_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags);
  460. if (sk->sk_err || (sk->sk_shutdown & SEND_SHUTDOWN))
  461. return -EPIPE;
  462. more = msg->msg_flags & MSG_MORE;
  463. skb = call->tx_pending;
  464. call->tx_pending = NULL;
  465. copied = 0;
  466. if (len > iov_iter_count(&msg->msg_iter))
  467. len = iov_iter_count(&msg->msg_iter);
  468. while (len) {
  469. int copy;
  470. if (!skb) {
  471. size_t size, chunk, max, space;
  472. _debug("alloc");
  473. if (CIRC_SPACE(call->acks_head, call->acks_tail,
  474. call->acks_winsz) <= 0) {
  475. ret = -EAGAIN;
  476. if (msg->msg_flags & MSG_DONTWAIT)
  477. goto maybe_error;
  478. ret = rxrpc_wait_for_tx_window(rx, call,
  479. &timeo);
  480. if (ret < 0)
  481. goto maybe_error;
  482. }
  483. max = call->conn->trans->peer->maxdata;
  484. max -= call->conn->security_size;
  485. max &= ~(call->conn->size_align - 1UL);
  486. chunk = max;
  487. if (chunk > len && !more)
  488. chunk = len;
  489. space = chunk + call->conn->size_align;
  490. space &= ~(call->conn->size_align - 1UL);
  491. size = space + call->conn->header_size;
  492. _debug("SIZE: %zu/%zu/%zu", chunk, space, size);
  493. /* create a buffer that we can retain until it's ACK'd */
  494. skb = sock_alloc_send_skb(
  495. sk, size, msg->msg_flags & MSG_DONTWAIT, &ret);
  496. if (!skb)
  497. goto maybe_error;
  498. rxrpc_new_skb(skb);
  499. _debug("ALLOC SEND %p", skb);
  500. ASSERTCMP(skb->mark, ==, 0);
  501. _debug("HS: %u", call->conn->header_size);
  502. skb_reserve(skb, call->conn->header_size);
  503. skb->len += call->conn->header_size;
  504. sp = rxrpc_skb(skb);
  505. sp->remain = chunk;
  506. if (sp->remain > skb_tailroom(skb))
  507. sp->remain = skb_tailroom(skb);
  508. _net("skb: hr %d, tr %d, hl %d, rm %d",
  509. skb_headroom(skb),
  510. skb_tailroom(skb),
  511. skb_headlen(skb),
  512. sp->remain);
  513. skb->ip_summed = CHECKSUM_UNNECESSARY;
  514. }
  515. _debug("append");
  516. sp = rxrpc_skb(skb);
  517. /* append next segment of data to the current buffer */
  518. copy = skb_tailroom(skb);
  519. ASSERTCMP(copy, >, 0);
  520. if (copy > len)
  521. copy = len;
  522. if (copy > sp->remain)
  523. copy = sp->remain;
  524. _debug("add");
  525. ret = skb_add_data(skb, &msg->msg_iter, copy);
  526. _debug("added");
  527. if (ret < 0)
  528. goto efault;
  529. sp->remain -= copy;
  530. skb->mark += copy;
  531. copied += copy;
  532. len -= copy;
  533. /* check for the far side aborting the call or a network error
  534. * occurring */
  535. if (call->state > RXRPC_CALL_COMPLETE)
  536. goto call_aborted;
  537. /* add the packet to the send queue if it's now full */
  538. if (sp->remain <= 0 || (!len && !more)) {
  539. struct rxrpc_connection *conn = call->conn;
  540. uint32_t seq;
  541. size_t pad;
  542. /* pad out if we're using security */
  543. if (conn->security) {
  544. pad = conn->security_size + skb->mark;
  545. pad = conn->size_align - pad;
  546. pad &= conn->size_align - 1;
  547. _debug("pad %zu", pad);
  548. if (pad)
  549. memset(skb_put(skb, pad), 0, pad);
  550. }
  551. seq = atomic_inc_return(&call->sequence);
  552. sp->hdr.epoch = conn->epoch;
  553. sp->hdr.cid = call->cid;
  554. sp->hdr.callNumber = call->call_id;
  555. sp->hdr.seq = htonl(seq);
  556. sp->hdr.serial =
  557. htonl(atomic_inc_return(&conn->serial));
  558. sp->hdr.type = RXRPC_PACKET_TYPE_DATA;
  559. sp->hdr.userStatus = 0;
  560. sp->hdr.securityIndex = conn->security_ix;
  561. sp->hdr._rsvd = 0;
  562. sp->hdr.serviceId = conn->service_id;
  563. sp->hdr.flags = conn->out_clientflag;
  564. if (len == 0 && !more)
  565. sp->hdr.flags |= RXRPC_LAST_PACKET;
  566. else if (CIRC_SPACE(call->acks_head, call->acks_tail,
  567. call->acks_winsz) > 1)
  568. sp->hdr.flags |= RXRPC_MORE_PACKETS;
  569. if (more && seq & 1)
  570. sp->hdr.flags |= RXRPC_REQUEST_ACK;
  571. ret = rxrpc_secure_packet(
  572. call, skb, skb->mark,
  573. skb->head + sizeof(struct rxrpc_header));
  574. if (ret < 0)
  575. goto out;
  576. memcpy(skb->head, &sp->hdr,
  577. sizeof(struct rxrpc_header));
  578. rxrpc_queue_packet(call, skb, !iov_iter_count(&msg->msg_iter) && !more);
  579. skb = NULL;
  580. }
  581. }
  582. success:
  583. ret = copied;
  584. out:
  585. call->tx_pending = skb;
  586. _leave(" = %d", ret);
  587. return ret;
  588. call_aborted:
  589. rxrpc_free_skb(skb);
  590. if (call->state == RXRPC_CALL_NETWORK_ERROR)
  591. ret = call->conn->trans->peer->net_error;
  592. else
  593. ret = -ECONNABORTED;
  594. _leave(" = %d", ret);
  595. return ret;
  596. maybe_error:
  597. if (copied)
  598. goto success;
  599. goto out;
  600. efault:
  601. ret = -EFAULT;
  602. goto out;
  603. }