ar-output.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746
  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 (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) {
  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. mm_segment_t oldfs = get_fs();
  204. set_fs(KERNEL_DS);
  205. ret = rxrpc_send_data(NULL, call->socket, call, msg, len);
  206. set_fs(oldfs);
  207. }
  208. release_sock(&call->socket->sk);
  209. _leave(" = %d", ret);
  210. return ret;
  211. }
  212. EXPORT_SYMBOL(rxrpc_kernel_send_data);
  213. /**
  214. * rxrpc_kernel_abort_call - Allow a kernel service to abort a call
  215. * @call: The call to be aborted
  216. * @abort_code: The abort code to stick into the ABORT packet
  217. *
  218. * Allow a kernel service to abort a call, if it's still in an abortable state.
  219. */
  220. void rxrpc_kernel_abort_call(struct rxrpc_call *call, u32 abort_code)
  221. {
  222. _enter("{%d},%d", call->debug_id, abort_code);
  223. lock_sock(&call->socket->sk);
  224. _debug("CALL %d USR %lx ST %d on CONN %p",
  225. call->debug_id, call->user_call_ID, call->state, call->conn);
  226. if (call->state < RXRPC_CALL_COMPLETE)
  227. rxrpc_send_abort(call, abort_code);
  228. release_sock(&call->socket->sk);
  229. _leave("");
  230. }
  231. EXPORT_SYMBOL(rxrpc_kernel_abort_call);
  232. /*
  233. * send a message through a server socket
  234. * - caller holds the socket locked
  235. */
  236. int rxrpc_server_sendmsg(struct kiocb *iocb, struct rxrpc_sock *rx,
  237. struct msghdr *msg, size_t len)
  238. {
  239. enum rxrpc_command cmd;
  240. struct rxrpc_call *call;
  241. unsigned long user_call_ID = 0;
  242. u32 abort_code = 0;
  243. int ret;
  244. _enter("");
  245. ret = rxrpc_sendmsg_cmsg(rx, msg, &user_call_ID, &cmd, &abort_code,
  246. true);
  247. if (ret < 0)
  248. return ret;
  249. if (cmd == RXRPC_CMD_ACCEPT) {
  250. call = rxrpc_accept_call(rx, user_call_ID);
  251. if (IS_ERR(call))
  252. return PTR_ERR(call);
  253. rxrpc_put_call(call);
  254. return 0;
  255. }
  256. call = rxrpc_find_server_call(rx, user_call_ID);
  257. if (!call)
  258. return -EBADSLT;
  259. if (call->state >= RXRPC_CALL_COMPLETE) {
  260. ret = -ESHUTDOWN;
  261. goto out;
  262. }
  263. switch (cmd) {
  264. case RXRPC_CMD_SEND_DATA:
  265. if (call->state != RXRPC_CALL_CLIENT_SEND_REQUEST &&
  266. call->state != RXRPC_CALL_SERVER_ACK_REQUEST &&
  267. call->state != RXRPC_CALL_SERVER_SEND_REPLY) {
  268. /* Tx phase not yet begun for this call */
  269. ret = -EPROTO;
  270. break;
  271. }
  272. ret = rxrpc_send_data(iocb, rx, call, msg, len);
  273. break;
  274. case RXRPC_CMD_SEND_ABORT:
  275. rxrpc_send_abort(call, abort_code);
  276. break;
  277. default:
  278. BUG();
  279. }
  280. out:
  281. rxrpc_put_call(call);
  282. _leave(" = %d", ret);
  283. return ret;
  284. }
  285. /*
  286. * send a packet through the transport endpoint
  287. */
  288. int rxrpc_send_packet(struct rxrpc_transport *trans, struct sk_buff *skb)
  289. {
  290. struct kvec iov[1];
  291. struct msghdr msg;
  292. int ret, opt;
  293. _enter(",{%d}", skb->len);
  294. iov[0].iov_base = skb->head;
  295. iov[0].iov_len = skb->len;
  296. msg.msg_name = &trans->peer->srx.transport.sin;
  297. msg.msg_namelen = sizeof(trans->peer->srx.transport.sin);
  298. msg.msg_control = NULL;
  299. msg.msg_controllen = 0;
  300. msg.msg_flags = 0;
  301. /* send the packet with the don't fragment bit set if we currently
  302. * think it's small enough */
  303. if (skb->len - sizeof(struct rxrpc_header) < trans->peer->maxdata) {
  304. down_read(&trans->local->defrag_sem);
  305. /* send the packet by UDP
  306. * - returns -EMSGSIZE if UDP would have to fragment the packet
  307. * to go out of the interface
  308. * - in which case, we'll have processed the ICMP error
  309. * message and update the peer record
  310. */
  311. ret = kernel_sendmsg(trans->local->socket, &msg, iov, 1,
  312. iov[0].iov_len);
  313. up_read(&trans->local->defrag_sem);
  314. if (ret == -EMSGSIZE)
  315. goto send_fragmentable;
  316. _leave(" = %d [%u]", ret, trans->peer->maxdata);
  317. return ret;
  318. }
  319. send_fragmentable:
  320. /* attempt to send this message with fragmentation enabled */
  321. _debug("send fragment");
  322. down_write(&trans->local->defrag_sem);
  323. opt = IP_PMTUDISC_DONT;
  324. ret = kernel_setsockopt(trans->local->socket, SOL_IP, IP_MTU_DISCOVER,
  325. (char *) &opt, sizeof(opt));
  326. if (ret == 0) {
  327. ret = kernel_sendmsg(trans->local->socket, &msg, iov, 1,
  328. iov[0].iov_len);
  329. opt = IP_PMTUDISC_DO;
  330. kernel_setsockopt(trans->local->socket, SOL_IP,
  331. IP_MTU_DISCOVER, (char *) &opt, sizeof(opt));
  332. }
  333. up_write(&trans->local->defrag_sem);
  334. _leave(" = %d [frag %u]", ret, trans->peer->maxdata);
  335. return ret;
  336. }
  337. /*
  338. * wait for space to appear in the transmit/ACK window
  339. * - caller holds the socket locked
  340. */
  341. static int rxrpc_wait_for_tx_window(struct rxrpc_sock *rx,
  342. struct rxrpc_call *call,
  343. long *timeo)
  344. {
  345. DECLARE_WAITQUEUE(myself, current);
  346. int ret;
  347. _enter(",{%d},%ld",
  348. CIRC_SPACE(call->acks_head, call->acks_tail, call->acks_winsz),
  349. *timeo);
  350. add_wait_queue(&call->tx_waitq, &myself);
  351. for (;;) {
  352. set_current_state(TASK_INTERRUPTIBLE);
  353. ret = 0;
  354. if (CIRC_SPACE(call->acks_head, call->acks_tail,
  355. call->acks_winsz) > 0)
  356. break;
  357. if (signal_pending(current)) {
  358. ret = sock_intr_errno(*timeo);
  359. break;
  360. }
  361. release_sock(&rx->sk);
  362. *timeo = schedule_timeout(*timeo);
  363. lock_sock(&rx->sk);
  364. }
  365. remove_wait_queue(&call->tx_waitq, &myself);
  366. set_current_state(TASK_RUNNING);
  367. _leave(" = %d", ret);
  368. return ret;
  369. }
  370. /*
  371. * attempt to schedule an instant Tx resend
  372. */
  373. static inline void rxrpc_instant_resend(struct rxrpc_call *call)
  374. {
  375. read_lock_bh(&call->state_lock);
  376. if (try_to_del_timer_sync(&call->resend_timer) >= 0) {
  377. clear_bit(RXRPC_CALL_RUN_RTIMER, &call->flags);
  378. if (call->state < RXRPC_CALL_COMPLETE &&
  379. !test_and_set_bit(RXRPC_CALL_RESEND_TIMER, &call->events))
  380. rxrpc_queue_call(call);
  381. }
  382. read_unlock_bh(&call->state_lock);
  383. }
  384. /*
  385. * queue a packet for transmission, set the resend timer and attempt
  386. * to send the packet immediately
  387. */
  388. static void rxrpc_queue_packet(struct rxrpc_call *call, struct sk_buff *skb,
  389. bool last)
  390. {
  391. struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
  392. int ret;
  393. _net("queue skb %p [%d]", skb, call->acks_head);
  394. ASSERT(call->acks_window != NULL);
  395. call->acks_window[call->acks_head] = (unsigned long) skb;
  396. smp_wmb();
  397. call->acks_head = (call->acks_head + 1) & (call->acks_winsz - 1);
  398. if (last || call->state == RXRPC_CALL_SERVER_ACK_REQUEST) {
  399. _debug("________awaiting reply/ACK__________");
  400. write_lock_bh(&call->state_lock);
  401. switch (call->state) {
  402. case RXRPC_CALL_CLIENT_SEND_REQUEST:
  403. call->state = RXRPC_CALL_CLIENT_AWAIT_REPLY;
  404. break;
  405. case RXRPC_CALL_SERVER_ACK_REQUEST:
  406. call->state = RXRPC_CALL_SERVER_SEND_REPLY;
  407. if (!last)
  408. break;
  409. case RXRPC_CALL_SERVER_SEND_REPLY:
  410. call->state = RXRPC_CALL_SERVER_AWAIT_ACK;
  411. break;
  412. default:
  413. break;
  414. }
  415. write_unlock_bh(&call->state_lock);
  416. }
  417. _proto("Tx DATA %%%u { #%u }",
  418. ntohl(sp->hdr.serial), ntohl(sp->hdr.seq));
  419. sp->need_resend = false;
  420. sp->resend_at = jiffies + rxrpc_resend_timeout;
  421. if (!test_and_set_bit(RXRPC_CALL_RUN_RTIMER, &call->flags)) {
  422. _debug("run timer");
  423. call->resend_timer.expires = sp->resend_at;
  424. add_timer(&call->resend_timer);
  425. }
  426. /* attempt to cancel the rx-ACK timer, deferring reply transmission if
  427. * we're ACK'ing the request phase of an incoming call */
  428. ret = -EAGAIN;
  429. if (try_to_del_timer_sync(&call->ack_timer) >= 0) {
  430. /* the packet may be freed by rxrpc_process_call() before this
  431. * returns */
  432. ret = rxrpc_send_packet(call->conn->trans, skb);
  433. _net("sent skb %p", skb);
  434. } else {
  435. _debug("failed to delete ACK timer");
  436. }
  437. if (ret < 0) {
  438. _debug("need instant resend %d", ret);
  439. sp->need_resend = true;
  440. rxrpc_instant_resend(call);
  441. }
  442. _leave("");
  443. }
  444. /*
  445. * send data through a socket
  446. * - must be called in process context
  447. * - caller holds the socket locked
  448. */
  449. static int rxrpc_send_data(struct kiocb *iocb,
  450. struct rxrpc_sock *rx,
  451. struct rxrpc_call *call,
  452. struct msghdr *msg, size_t len)
  453. {
  454. struct rxrpc_skb_priv *sp;
  455. unsigned char __user *from;
  456. struct sk_buff *skb;
  457. struct iovec *iov;
  458. struct sock *sk = &rx->sk;
  459. long timeo;
  460. bool more;
  461. int ret, ioc, segment, copied;
  462. _enter(",,,{%zu},%zu", msg->msg_iovlen, len);
  463. timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
  464. /* this should be in poll */
  465. clear_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags);
  466. if (sk->sk_err || (sk->sk_shutdown & SEND_SHUTDOWN))
  467. return -EPIPE;
  468. iov = msg->msg_iov;
  469. ioc = msg->msg_iovlen - 1;
  470. from = iov->iov_base;
  471. segment = iov->iov_len;
  472. iov++;
  473. more = msg->msg_flags & MSG_MORE;
  474. skb = call->tx_pending;
  475. call->tx_pending = NULL;
  476. copied = 0;
  477. do {
  478. int copy;
  479. if (segment > len)
  480. segment = len;
  481. _debug("SEGMENT %d @%p", segment, from);
  482. if (!skb) {
  483. size_t size, chunk, max, space;
  484. _debug("alloc");
  485. if (CIRC_SPACE(call->acks_head, call->acks_tail,
  486. call->acks_winsz) <= 0) {
  487. ret = -EAGAIN;
  488. if (msg->msg_flags & MSG_DONTWAIT)
  489. goto maybe_error;
  490. ret = rxrpc_wait_for_tx_window(rx, call,
  491. &timeo);
  492. if (ret < 0)
  493. goto maybe_error;
  494. }
  495. max = call->conn->trans->peer->maxdata;
  496. max -= call->conn->security_size;
  497. max &= ~(call->conn->size_align - 1UL);
  498. chunk = max;
  499. if (chunk > len && !more)
  500. chunk = len;
  501. space = chunk + call->conn->size_align;
  502. space &= ~(call->conn->size_align - 1UL);
  503. size = space + call->conn->header_size;
  504. _debug("SIZE: %zu/%zu/%zu", chunk, space, size);
  505. /* create a buffer that we can retain until it's ACK'd */
  506. skb = sock_alloc_send_skb(
  507. sk, size, msg->msg_flags & MSG_DONTWAIT, &ret);
  508. if (!skb)
  509. goto maybe_error;
  510. rxrpc_new_skb(skb);
  511. _debug("ALLOC SEND %p", skb);
  512. ASSERTCMP(skb->mark, ==, 0);
  513. _debug("HS: %u", call->conn->header_size);
  514. skb_reserve(skb, call->conn->header_size);
  515. skb->len += call->conn->header_size;
  516. sp = rxrpc_skb(skb);
  517. sp->remain = chunk;
  518. if (sp->remain > skb_tailroom(skb))
  519. sp->remain = skb_tailroom(skb);
  520. _net("skb: hr %d, tr %d, hl %d, rm %d",
  521. skb_headroom(skb),
  522. skb_tailroom(skb),
  523. skb_headlen(skb),
  524. sp->remain);
  525. skb->ip_summed = CHECKSUM_UNNECESSARY;
  526. }
  527. _debug("append");
  528. sp = rxrpc_skb(skb);
  529. /* append next segment of data to the current buffer */
  530. copy = skb_tailroom(skb);
  531. ASSERTCMP(copy, >, 0);
  532. if (copy > segment)
  533. copy = segment;
  534. if (copy > sp->remain)
  535. copy = sp->remain;
  536. _debug("add");
  537. ret = skb_add_data(skb, from, copy);
  538. _debug("added");
  539. if (ret < 0)
  540. goto efault;
  541. sp->remain -= copy;
  542. skb->mark += copy;
  543. copied += copy;
  544. len -= copy;
  545. segment -= copy;
  546. from += copy;
  547. while (segment == 0 && ioc > 0) {
  548. from = iov->iov_base;
  549. segment = iov->iov_len;
  550. iov++;
  551. ioc--;
  552. }
  553. if (len == 0) {
  554. segment = 0;
  555. ioc = 0;
  556. }
  557. /* check for the far side aborting the call or a network error
  558. * occurring */
  559. if (call->state > RXRPC_CALL_COMPLETE)
  560. goto call_aborted;
  561. /* add the packet to the send queue if it's now full */
  562. if (sp->remain <= 0 || (segment == 0 && !more)) {
  563. struct rxrpc_connection *conn = call->conn;
  564. uint32_t seq;
  565. size_t pad;
  566. /* pad out if we're using security */
  567. if (conn->security) {
  568. pad = conn->security_size + skb->mark;
  569. pad = conn->size_align - pad;
  570. pad &= conn->size_align - 1;
  571. _debug("pad %zu", pad);
  572. if (pad)
  573. memset(skb_put(skb, pad), 0, pad);
  574. }
  575. seq = atomic_inc_return(&call->sequence);
  576. sp->hdr.epoch = conn->epoch;
  577. sp->hdr.cid = call->cid;
  578. sp->hdr.callNumber = call->call_id;
  579. sp->hdr.seq = htonl(seq);
  580. sp->hdr.serial =
  581. htonl(atomic_inc_return(&conn->serial));
  582. sp->hdr.type = RXRPC_PACKET_TYPE_DATA;
  583. sp->hdr.userStatus = 0;
  584. sp->hdr.securityIndex = conn->security_ix;
  585. sp->hdr._rsvd = 0;
  586. sp->hdr.serviceId = conn->service_id;
  587. sp->hdr.flags = conn->out_clientflag;
  588. if (len == 0 && !more)
  589. sp->hdr.flags |= RXRPC_LAST_PACKET;
  590. else if (CIRC_SPACE(call->acks_head, call->acks_tail,
  591. call->acks_winsz) > 1)
  592. sp->hdr.flags |= RXRPC_MORE_PACKETS;
  593. if (more && seq & 1)
  594. sp->hdr.flags |= RXRPC_REQUEST_ACK;
  595. ret = rxrpc_secure_packet(
  596. call, skb, skb->mark,
  597. skb->head + sizeof(struct rxrpc_header));
  598. if (ret < 0)
  599. goto out;
  600. memcpy(skb->head, &sp->hdr,
  601. sizeof(struct rxrpc_header));
  602. rxrpc_queue_packet(call, skb, segment == 0 && !more);
  603. skb = NULL;
  604. }
  605. } while (segment > 0);
  606. success:
  607. ret = copied;
  608. out:
  609. call->tx_pending = skb;
  610. _leave(" = %d", ret);
  611. return ret;
  612. call_aborted:
  613. rxrpc_free_skb(skb);
  614. if (call->state == RXRPC_CALL_NETWORK_ERROR)
  615. ret = call->conn->trans->peer->net_error;
  616. else
  617. ret = -ECONNABORTED;
  618. _leave(" = %d", ret);
  619. return ret;
  620. maybe_error:
  621. if (copied)
  622. goto success;
  623. goto out;
  624. efault:
  625. ret = -EFAULT;
  626. goto out;
  627. }