ar-output.c 18 KB

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