ar-output.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740
  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. int rxrpc_resend_timeout = 4;
  20. static int rxrpc_send_data(struct kiocb *iocb,
  21. struct rxrpc_sock *rx,
  22. struct rxrpc_call *call,
  23. struct msghdr *msg, size_t len);
  24. /*
  25. * extract control messages from the sendmsg() control buffer
  26. */
  27. static int rxrpc_sendmsg_cmsg(struct rxrpc_sock *rx, struct msghdr *msg,
  28. unsigned long *user_call_ID,
  29. enum rxrpc_command *command,
  30. u32 *abort_code,
  31. bool server)
  32. {
  33. struct cmsghdr *cmsg;
  34. int len;
  35. *command = RXRPC_CMD_SEND_DATA;
  36. if (msg->msg_controllen == 0)
  37. return -EINVAL;
  38. for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) {
  39. if (!CMSG_OK(msg, cmsg))
  40. return -EINVAL;
  41. len = cmsg->cmsg_len - CMSG_ALIGN(sizeof(struct cmsghdr));
  42. _debug("CMSG %d, %d, %d",
  43. cmsg->cmsg_level, cmsg->cmsg_type, len);
  44. if (cmsg->cmsg_level != SOL_RXRPC)
  45. continue;
  46. switch (cmsg->cmsg_type) {
  47. case RXRPC_USER_CALL_ID:
  48. if (msg->msg_flags & MSG_CMSG_COMPAT) {
  49. if (len != sizeof(u32))
  50. return -EINVAL;
  51. *user_call_ID = *(u32 *) CMSG_DATA(cmsg);
  52. } else {
  53. if (len != sizeof(unsigned long))
  54. return -EINVAL;
  55. *user_call_ID = *(unsigned long *)
  56. CMSG_DATA(cmsg);
  57. }
  58. _debug("User Call ID %lx", *user_call_ID);
  59. break;
  60. case RXRPC_ABORT:
  61. if (*command != RXRPC_CMD_SEND_DATA)
  62. return -EINVAL;
  63. *command = RXRPC_CMD_SEND_ABORT;
  64. if (len != sizeof(*abort_code))
  65. return -EINVAL;
  66. *abort_code = *(unsigned int *) CMSG_DATA(cmsg);
  67. _debug("Abort %x", *abort_code);
  68. if (*abort_code == 0)
  69. return -EINVAL;
  70. break;
  71. case RXRPC_ACCEPT:
  72. if (*command != RXRPC_CMD_SEND_DATA)
  73. return -EINVAL;
  74. *command = RXRPC_CMD_ACCEPT;
  75. if (len != 0)
  76. return -EINVAL;
  77. if (!server)
  78. return -EISCONN;
  79. break;
  80. default:
  81. return -EINVAL;
  82. }
  83. }
  84. _leave(" = 0");
  85. return 0;
  86. }
  87. /*
  88. * abort a call, sending an ABORT packet to the peer
  89. */
  90. static void rxrpc_send_abort(struct rxrpc_call *call, u32 abort_code)
  91. {
  92. write_lock_bh(&call->state_lock);
  93. if (call->state <= RXRPC_CALL_COMPLETE) {
  94. call->state = RXRPC_CALL_LOCALLY_ABORTED;
  95. call->abort_code = abort_code;
  96. set_bit(RXRPC_CALL_ABORT, &call->events);
  97. del_timer_sync(&call->resend_timer);
  98. del_timer_sync(&call->ack_timer);
  99. clear_bit(RXRPC_CALL_RESEND_TIMER, &call->events);
  100. clear_bit(RXRPC_CALL_ACK, &call->events);
  101. clear_bit(RXRPC_CALL_RUN_RTIMER, &call->flags);
  102. rxrpc_queue_call(call);
  103. }
  104. write_unlock_bh(&call->state_lock);
  105. }
  106. /*
  107. * send a message forming part of a client call through an RxRPC socket
  108. * - caller holds the socket locked
  109. * - the socket may be either a client socket or a server socket
  110. */
  111. int rxrpc_client_sendmsg(struct kiocb *iocb, struct rxrpc_sock *rx,
  112. struct rxrpc_transport *trans, struct msghdr *msg,
  113. size_t len)
  114. {
  115. struct rxrpc_conn_bundle *bundle;
  116. enum rxrpc_command cmd;
  117. struct rxrpc_call *call;
  118. unsigned long user_call_ID = 0;
  119. struct key *key;
  120. __be16 service_id;
  121. u32 abort_code = 0;
  122. int ret;
  123. _enter("");
  124. ASSERT(trans != NULL);
  125. ret = rxrpc_sendmsg_cmsg(rx, msg, &user_call_ID, &cmd, &abort_code,
  126. false);
  127. if (ret < 0)
  128. return ret;
  129. bundle = NULL;
  130. if (trans) {
  131. service_id = rx->service_id;
  132. if (msg->msg_name) {
  133. struct sockaddr_rxrpc *srx =
  134. (struct sockaddr_rxrpc *) msg->msg_name;
  135. service_id = htons(srx->srx_service);
  136. }
  137. key = rx->key;
  138. if (key && !rx->key->payload.data)
  139. key = NULL;
  140. bundle = rxrpc_get_bundle(rx, trans, key, service_id,
  141. GFP_KERNEL);
  142. if (IS_ERR(bundle))
  143. return PTR_ERR(bundle);
  144. }
  145. call = rxrpc_get_client_call(rx, trans, bundle, user_call_ID,
  146. abort_code == 0, GFP_KERNEL);
  147. if (trans)
  148. rxrpc_put_bundle(trans, bundle);
  149. if (IS_ERR(call)) {
  150. _leave(" = %ld", PTR_ERR(call));
  151. return PTR_ERR(call);
  152. }
  153. _debug("CALL %d USR %lx ST %d on CONN %p",
  154. call->debug_id, call->user_call_ID, call->state, call->conn);
  155. if (call->state >= RXRPC_CALL_COMPLETE) {
  156. /* it's too late for this call */
  157. ret = -ESHUTDOWN;
  158. } else if (cmd == RXRPC_CMD_SEND_ABORT) {
  159. rxrpc_send_abort(call, abort_code);
  160. } else if (cmd != RXRPC_CMD_SEND_DATA) {
  161. ret = -EINVAL;
  162. } else if (call->state != RXRPC_CALL_CLIENT_SEND_REQUEST) {
  163. /* request phase complete for this client call */
  164. ret = -EPROTO;
  165. } else {
  166. ret = rxrpc_send_data(iocb, rx, call, msg, len);
  167. }
  168. rxrpc_put_call(call);
  169. _leave(" = %d", ret);
  170. return ret;
  171. }
  172. /**
  173. * rxrpc_kernel_send_data - Allow a kernel service to send data on a call
  174. * @call: The call to send data through
  175. * @msg: The data to send
  176. * @len: The amount of data to send
  177. *
  178. * Allow a kernel service to send data on a call. The call must be in an state
  179. * appropriate to sending data. No control data should be supplied in @msg,
  180. * nor should an address be supplied. MSG_MORE should be flagged if there's
  181. * more data to come, otherwise this data will end the transmission phase.
  182. */
  183. int rxrpc_kernel_send_data(struct rxrpc_call *call, struct msghdr *msg,
  184. size_t len)
  185. {
  186. int ret;
  187. _enter("{%d,%s},", call->debug_id, rxrpc_call_states[call->state]);
  188. ASSERTCMP(msg->msg_name, ==, NULL);
  189. ASSERTCMP(msg->msg_control, ==, NULL);
  190. lock_sock(&call->socket->sk);
  191. _debug("CALL %d USR %lx ST %d on CONN %p",
  192. call->debug_id, call->user_call_ID, call->state, call->conn);
  193. if (call->state >= RXRPC_CALL_COMPLETE) {
  194. ret = -ESHUTDOWN; /* it's too late for this call */
  195. } else if (call->state != RXRPC_CALL_CLIENT_SEND_REQUEST &&
  196. call->state != RXRPC_CALL_SERVER_ACK_REQUEST &&
  197. call->state != RXRPC_CALL_SERVER_SEND_REPLY) {
  198. ret = -EPROTO; /* request phase complete for this client call */
  199. } else {
  200. mm_segment_t oldfs = get_fs();
  201. set_fs(KERNEL_DS);
  202. ret = rxrpc_send_data(NULL, call->socket, call, msg, len);
  203. set_fs(oldfs);
  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 * HZ;
  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. unsigned char __user *from;
  453. struct sk_buff *skb;
  454. struct iovec *iov;
  455. struct sock *sk = &rx->sk;
  456. long timeo;
  457. bool more;
  458. int ret, ioc, segment, copied;
  459. _enter(",,,{%zu},%zu", msg->msg_iovlen, len);
  460. timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
  461. /* this should be in poll */
  462. clear_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags);
  463. if (sk->sk_err || (sk->sk_shutdown & SEND_SHUTDOWN))
  464. return -EPIPE;
  465. iov = msg->msg_iov;
  466. ioc = msg->msg_iovlen - 1;
  467. from = iov->iov_base;
  468. segment = iov->iov_len;
  469. iov++;
  470. more = msg->msg_flags & MSG_MORE;
  471. skb = call->tx_pending;
  472. call->tx_pending = NULL;
  473. copied = 0;
  474. do {
  475. int copy;
  476. if (segment > len)
  477. segment = len;
  478. _debug("SEGMENT %d @%p", segment, from);
  479. if (!skb) {
  480. size_t size, chunk, max, space;
  481. _debug("alloc");
  482. if (CIRC_SPACE(call->acks_head, call->acks_tail,
  483. call->acks_winsz) <= 0) {
  484. ret = -EAGAIN;
  485. if (msg->msg_flags & MSG_DONTWAIT)
  486. goto maybe_error;
  487. ret = rxrpc_wait_for_tx_window(rx, call,
  488. &timeo);
  489. if (ret < 0)
  490. goto maybe_error;
  491. }
  492. max = call->conn->trans->peer->maxdata;
  493. max -= call->conn->security_size;
  494. max &= ~(call->conn->size_align - 1UL);
  495. chunk = max;
  496. if (chunk > len && !more)
  497. chunk = len;
  498. space = chunk + call->conn->size_align;
  499. space &= ~(call->conn->size_align - 1UL);
  500. size = space + call->conn->header_size;
  501. _debug("SIZE: %zu/%zu/%zu", chunk, space, size);
  502. /* create a buffer that we can retain until it's ACK'd */
  503. skb = sock_alloc_send_skb(
  504. sk, size, msg->msg_flags & MSG_DONTWAIT, &ret);
  505. if (!skb)
  506. goto maybe_error;
  507. rxrpc_new_skb(skb);
  508. _debug("ALLOC SEND %p", skb);
  509. ASSERTCMP(skb->mark, ==, 0);
  510. _debug("HS: %u", call->conn->header_size);
  511. skb_reserve(skb, call->conn->header_size);
  512. skb->len += call->conn->header_size;
  513. sp = rxrpc_skb(skb);
  514. sp->remain = chunk;
  515. if (sp->remain > skb_tailroom(skb))
  516. sp->remain = skb_tailroom(skb);
  517. _net("skb: hr %d, tr %d, hl %d, rm %d",
  518. skb_headroom(skb),
  519. skb_tailroom(skb),
  520. skb_headlen(skb),
  521. sp->remain);
  522. skb->ip_summed = CHECKSUM_UNNECESSARY;
  523. }
  524. _debug("append");
  525. sp = rxrpc_skb(skb);
  526. /* append next segment of data to the current buffer */
  527. copy = skb_tailroom(skb);
  528. ASSERTCMP(copy, >, 0);
  529. if (copy > segment)
  530. copy = segment;
  531. if (copy > sp->remain)
  532. copy = sp->remain;
  533. _debug("add");
  534. ret = skb_add_data(skb, from, copy);
  535. _debug("added");
  536. if (ret < 0)
  537. goto efault;
  538. sp->remain -= copy;
  539. skb->mark += copy;
  540. copied += copy;
  541. len -= copy;
  542. segment -= copy;
  543. from += copy;
  544. while (segment == 0 && ioc > 0) {
  545. from = iov->iov_base;
  546. segment = iov->iov_len;
  547. iov++;
  548. ioc--;
  549. }
  550. if (len == 0) {
  551. segment = 0;
  552. ioc = 0;
  553. }
  554. /* check for the far side aborting the call or a network error
  555. * occurring */
  556. if (call->state > RXRPC_CALL_COMPLETE)
  557. goto call_aborted;
  558. /* add the packet to the send queue if it's now full */
  559. if (sp->remain <= 0 || (segment == 0 && !more)) {
  560. struct rxrpc_connection *conn = call->conn;
  561. size_t pad;
  562. /* pad out if we're using security */
  563. if (conn->security) {
  564. pad = conn->security_size + skb->mark;
  565. pad = conn->size_align - pad;
  566. pad &= conn->size_align - 1;
  567. _debug("pad %zu", pad);
  568. if (pad)
  569. memset(skb_put(skb, pad), 0, pad);
  570. }
  571. sp->hdr.epoch = conn->epoch;
  572. sp->hdr.cid = call->cid;
  573. sp->hdr.callNumber = call->call_id;
  574. sp->hdr.seq =
  575. htonl(atomic_inc_return(&call->sequence));
  576. sp->hdr.serial =
  577. htonl(atomic_inc_return(&conn->serial));
  578. sp->hdr.type = RXRPC_PACKET_TYPE_DATA;
  579. sp->hdr.userStatus = 0;
  580. sp->hdr.securityIndex = conn->security_ix;
  581. sp->hdr._rsvd = 0;
  582. sp->hdr.serviceId = conn->service_id;
  583. sp->hdr.flags = conn->out_clientflag;
  584. if (len == 0 && !more)
  585. sp->hdr.flags |= RXRPC_LAST_PACKET;
  586. else if (CIRC_SPACE(call->acks_head, call->acks_tail,
  587. call->acks_winsz) > 1)
  588. sp->hdr.flags |= RXRPC_MORE_PACKETS;
  589. ret = rxrpc_secure_packet(
  590. call, skb, skb->mark,
  591. skb->head + sizeof(struct rxrpc_header));
  592. if (ret < 0)
  593. goto out;
  594. memcpy(skb->head, &sp->hdr,
  595. sizeof(struct rxrpc_header));
  596. rxrpc_queue_packet(call, skb, segment == 0 && !more);
  597. skb = NULL;
  598. }
  599. } while (segment > 0);
  600. success:
  601. ret = copied;
  602. out:
  603. call->tx_pending = skb;
  604. _leave(" = %d", ret);
  605. return ret;
  606. call_aborted:
  607. rxrpc_free_skb(skb);
  608. if (call->state == RXRPC_CALL_NETWORK_ERROR)
  609. ret = call->conn->trans->peer->net_error;
  610. else
  611. ret = -ECONNABORTED;
  612. _leave(" = %d", ret);
  613. return ret;
  614. maybe_error:
  615. if (copied)
  616. goto success;
  617. goto out;
  618. efault:
  619. ret = -EFAULT;
  620. goto out;
  621. }