46432e70a16b649fabd295a6a3f1d447de020c13
[platform/kernel/linux-rpi.git] / net / rxrpc / output.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* RxRPC packet transmission
3  *
4  * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
5  * Written by David Howells (dhowells@redhat.com)
6  */
7
8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9
10 #include <linux/net.h>
11 #include <linux/gfp.h>
12 #include <linux/skbuff.h>
13 #include <linux/export.h>
14 #include <net/sock.h>
15 #include <net/af_rxrpc.h>
16 #include <net/udp.h>
17 #include "ar-internal.h"
18
19 extern int udpv6_sendmsg(struct sock *sk, struct msghdr *msg, size_t len);
20
21 static ssize_t do_udp_sendmsg(struct socket *sk, struct msghdr *msg, size_t len)
22 {
23 #if IS_ENABLED(CONFIG_AF_RXRPC_IPV6)
24         struct sockaddr *sa = msg->msg_name;
25
26         if (sa->sa_family == AF_INET6)
27                 return udpv6_sendmsg(sk->sk, msg, len);
28 #endif
29         return udp_sendmsg(sk->sk, msg, len);
30 }
31
32 struct rxrpc_abort_buffer {
33         struct rxrpc_wire_header whdr;
34         __be32 abort_code;
35 };
36
37 static const char rxrpc_keepalive_string[] = "";
38
39 /*
40  * Increase Tx backoff on transmission failure and clear it on success.
41  */
42 static void rxrpc_tx_backoff(struct rxrpc_call *call, int ret)
43 {
44         if (ret < 0) {
45                 u16 tx_backoff = READ_ONCE(call->tx_backoff);
46
47                 if (tx_backoff < HZ)
48                         WRITE_ONCE(call->tx_backoff, tx_backoff + 1);
49         } else {
50                 WRITE_ONCE(call->tx_backoff, 0);
51         }
52 }
53
54 /*
55  * Arrange for a keepalive ping a certain time after we last transmitted.  This
56  * lets the far side know we're still interested in this call and helps keep
57  * the route through any intervening firewall open.
58  *
59  * Receiving a response to the ping will prevent the ->expect_rx_by timer from
60  * expiring.
61  */
62 static void rxrpc_set_keepalive(struct rxrpc_call *call)
63 {
64         unsigned long now = jiffies, keepalive_at = call->next_rx_timo / 6;
65
66         keepalive_at += now;
67         WRITE_ONCE(call->keepalive_at, keepalive_at);
68         rxrpc_reduce_call_timer(call, keepalive_at, now,
69                                 rxrpc_timer_set_for_keepalive);
70 }
71
72 /*
73  * Fill out an ACK packet.
74  */
75 static size_t rxrpc_fill_out_ack(struct rxrpc_connection *conn,
76                                  struct rxrpc_call *call,
77                                  struct rxrpc_txbuf *txb)
78 {
79         struct rxrpc_ackinfo ackinfo;
80         unsigned int qsize;
81         rxrpc_seq_t window, wtop, wrap_point, ix, first;
82         int rsize;
83         u64 wtmp;
84         u32 mtu, jmax;
85         u8 *ackp = txb->acks;
86         u8 sack_buffer[sizeof(call->ackr_sack_table)] __aligned(8);
87
88         atomic_set(&call->ackr_nr_unacked, 0);
89         atomic_set(&call->ackr_nr_consumed, 0);
90         rxrpc_inc_stat(call->rxnet, stat_tx_ack_fill);
91
92         /* Barrier against rxrpc_input_data(). */
93 retry:
94         wtmp   = atomic64_read_acquire(&call->ackr_window);
95         window = lower_32_bits(wtmp);
96         wtop   = upper_32_bits(wtmp);
97         txb->ack.firstPacket = htonl(window);
98         txb->ack.nAcks = 0;
99
100         if (after(wtop, window)) {
101                 /* Try to copy the SACK ring locklessly.  We can use the copy,
102                  * only if the now-current top of the window didn't go past the
103                  * previously read base - otherwise we can't know whether we
104                  * have old data or new data.
105                  */
106                 memcpy(sack_buffer, call->ackr_sack_table, sizeof(sack_buffer));
107                 wrap_point = window + RXRPC_SACK_SIZE - 1;
108                 wtmp   = atomic64_read_acquire(&call->ackr_window);
109                 window = lower_32_bits(wtmp);
110                 wtop   = upper_32_bits(wtmp);
111                 if (after(wtop, wrap_point)) {
112                         cond_resched();
113                         goto retry;
114                 }
115
116                 /* The buffer is maintained as a ring with an invariant mapping
117                  * between bit position and sequence number, so we'll probably
118                  * need to rotate it.
119                  */
120                 txb->ack.nAcks = wtop - window;
121                 ix = window % RXRPC_SACK_SIZE;
122                 first = sizeof(sack_buffer) - ix;
123
124                 if (ix + txb->ack.nAcks <= RXRPC_SACK_SIZE) {
125                         memcpy(txb->acks, sack_buffer + ix, txb->ack.nAcks);
126                 } else {
127                         memcpy(txb->acks, sack_buffer + ix, first);
128                         memcpy(txb->acks + first, sack_buffer,
129                                txb->ack.nAcks - first);
130                 }
131
132                 ackp += txb->ack.nAcks;
133         } else if (before(wtop, window)) {
134                 pr_warn("ack window backward %x %x", window, wtop);
135         } else if (txb->ack.reason == RXRPC_ACK_DELAY) {
136                 txb->ack.reason = RXRPC_ACK_IDLE;
137         }
138
139         mtu = conn->params.peer->if_mtu;
140         mtu -= conn->params.peer->hdrsize;
141         jmax = rxrpc_rx_jumbo_max;
142         qsize = (window - 1) - call->rx_consumed;
143         rsize = max_t(int, call->rx_winsize - qsize, 0);
144         ackinfo.rxMTU           = htonl(rxrpc_rx_mtu);
145         ackinfo.maxMTU          = htonl(mtu);
146         ackinfo.rwind           = htonl(rsize);
147         ackinfo.jumbo_max       = htonl(jmax);
148
149         *ackp++ = 0;
150         *ackp++ = 0;
151         *ackp++ = 0;
152         memcpy(ackp, &ackinfo, sizeof(ackinfo));
153         return txb->ack.nAcks + 3 + sizeof(ackinfo);
154 }
155
156 /*
157  * Record the beginning of an RTT probe.
158  */
159 static int rxrpc_begin_rtt_probe(struct rxrpc_call *call, rxrpc_serial_t serial,
160                                  enum rxrpc_rtt_tx_trace why)
161 {
162         unsigned long avail = call->rtt_avail;
163         int rtt_slot = 9;
164
165         if (!(avail & RXRPC_CALL_RTT_AVAIL_MASK))
166                 goto no_slot;
167
168         rtt_slot = __ffs(avail & RXRPC_CALL_RTT_AVAIL_MASK);
169         if (!test_and_clear_bit(rtt_slot, &call->rtt_avail))
170                 goto no_slot;
171
172         call->rtt_serial[rtt_slot] = serial;
173         call->rtt_sent_at[rtt_slot] = ktime_get_real();
174         smp_wmb(); /* Write data before avail bit */
175         set_bit(rtt_slot + RXRPC_CALL_RTT_PEND_SHIFT, &call->rtt_avail);
176
177         trace_rxrpc_rtt_tx(call, why, rtt_slot, serial);
178         return rtt_slot;
179
180 no_slot:
181         trace_rxrpc_rtt_tx(call, rxrpc_rtt_tx_no_slot, rtt_slot, serial);
182         return -1;
183 }
184
185 /*
186  * Cancel an RTT probe.
187  */
188 static void rxrpc_cancel_rtt_probe(struct rxrpc_call *call,
189                                    rxrpc_serial_t serial, int rtt_slot)
190 {
191         if (rtt_slot != -1) {
192                 clear_bit(rtt_slot + RXRPC_CALL_RTT_PEND_SHIFT, &call->rtt_avail);
193                 smp_wmb(); /* Clear pending bit before setting slot */
194                 set_bit(rtt_slot, &call->rtt_avail);
195                 trace_rxrpc_rtt_tx(call, rxrpc_rtt_tx_cancel, rtt_slot, serial);
196         }
197 }
198
199 /*
200  * Send an ACK call packet.
201  */
202 static int rxrpc_send_ack_packet(struct rxrpc_local *local, struct rxrpc_txbuf *txb)
203 {
204         struct rxrpc_connection *conn;
205         struct rxrpc_ack_buffer *pkt;
206         struct rxrpc_call *call = txb->call;
207         struct msghdr msg;
208         struct kvec iov[1];
209         rxrpc_serial_t serial;
210         size_t len, n;
211         int ret, rtt_slot = -1;
212
213         if (test_bit(RXRPC_CALL_DISCONNECTED, &call->flags))
214                 return -ECONNRESET;
215
216         conn = call->conn;
217
218         msg.msg_name    = &call->peer->srx.transport;
219         msg.msg_namelen = call->peer->srx.transport_len;
220         msg.msg_control = NULL;
221         msg.msg_controllen = 0;
222         msg.msg_flags   = 0;
223
224         if (txb->ack.reason == RXRPC_ACK_PING)
225                 txb->wire.flags |= RXRPC_REQUEST_ACK;
226
227         if (txb->ack.reason == RXRPC_ACK_DELAY)
228                 clear_bit(RXRPC_CALL_DELAY_ACK_PENDING, &call->flags);
229         if (txb->ack.reason == RXRPC_ACK_IDLE)
230                 clear_bit(RXRPC_CALL_IDLE_ACK_PENDING, &call->flags);
231
232         n = rxrpc_fill_out_ack(conn, call, txb);
233         if (n == 0)
234                 return 0;
235
236         iov[0].iov_base = &txb->wire;
237         iov[0].iov_len  = sizeof(txb->wire) + sizeof(txb->ack) + n;
238         len = iov[0].iov_len;
239
240         serial = atomic_inc_return(&conn->serial);
241         txb->wire.serial = htonl(serial);
242         trace_rxrpc_tx_ack(call->debug_id, serial,
243                            ntohl(txb->ack.firstPacket),
244                            ntohl(txb->ack.serial), txb->ack.reason, txb->ack.nAcks);
245         if (txb->ack_why == rxrpc_propose_ack_ping_for_lost_ack)
246                 call->acks_lost_ping = serial;
247
248         if (txb->ack.reason == RXRPC_ACK_PING)
249                 rtt_slot = rxrpc_begin_rtt_probe(call, serial, rxrpc_rtt_tx_ping);
250
251         rxrpc_inc_stat(call->rxnet, stat_tx_ack_send);
252
253         /* Grab the highest received seq as late as possible */
254         txb->ack.previousPacket = htonl(call->rx_highest_seq);
255
256         iov_iter_kvec(&msg.msg_iter, WRITE, iov, 1, len);
257         ret = do_udp_sendmsg(conn->params.local->socket, &msg, len);
258         call->peer->last_tx_at = ktime_get_seconds();
259         if (ret < 0)
260                 trace_rxrpc_tx_fail(call->debug_id, serial, ret,
261                                     rxrpc_tx_point_call_ack);
262         else
263                 trace_rxrpc_tx_packet(call->debug_id, &txb->wire,
264                                       rxrpc_tx_point_call_ack);
265         rxrpc_tx_backoff(call, ret);
266
267         if (call->state < RXRPC_CALL_COMPLETE) {
268                 if (ret < 0)
269                         rxrpc_cancel_rtt_probe(call, serial, rtt_slot);
270                 rxrpc_set_keepalive(call);
271         }
272
273         kfree(pkt);
274         return ret;
275 }
276
277 /*
278  * ACK transmitter for a local endpoint.  The UDP socket locks around each
279  * transmission, so we can only transmit one packet at a time, ACK, DATA or
280  * otherwise.
281  */
282 void rxrpc_transmit_ack_packets(struct rxrpc_local *local)
283 {
284         LIST_HEAD(queue);
285         int ret;
286
287         trace_rxrpc_local(local->debug_id, rxrpc_local_tx_ack,
288                           refcount_read(&local->ref), NULL);
289
290         if (list_empty(&local->ack_tx_queue))
291                 return;
292
293         spin_lock_bh(&local->ack_tx_lock);
294         list_splice_tail_init(&local->ack_tx_queue, &queue);
295         spin_unlock_bh(&local->ack_tx_lock);
296
297         while (!list_empty(&queue)) {
298                 struct rxrpc_txbuf *txb =
299                         list_entry(queue.next, struct rxrpc_txbuf, tx_link);
300
301                 ret = rxrpc_send_ack_packet(local, txb);
302                 if (ret < 0 && ret != -ECONNRESET) {
303                         spin_lock_bh(&local->ack_tx_lock);
304                         list_splice_init(&queue, &local->ack_tx_queue);
305                         spin_unlock_bh(&local->ack_tx_lock);
306                         break;
307                 }
308
309                 list_del_init(&txb->tx_link);
310                 rxrpc_put_call(txb->call, rxrpc_call_put);
311                 rxrpc_put_txbuf(txb, rxrpc_txbuf_put_ack_tx);
312         }
313 }
314
315 /*
316  * Send an ABORT call packet.
317  */
318 int rxrpc_send_abort_packet(struct rxrpc_call *call)
319 {
320         struct rxrpc_connection *conn;
321         struct rxrpc_abort_buffer pkt;
322         struct msghdr msg;
323         struct kvec iov[1];
324         rxrpc_serial_t serial;
325         int ret;
326
327         /* Don't bother sending aborts for a client call once the server has
328          * hard-ACK'd all of its request data.  After that point, we're not
329          * going to stop the operation proceeding, and whilst we might limit
330          * the reply, it's not worth it if we can send a new call on the same
331          * channel instead, thereby closing off this call.
332          */
333         if (rxrpc_is_client_call(call) &&
334             test_bit(RXRPC_CALL_TX_ALL_ACKED, &call->flags))
335                 return 0;
336
337         if (test_bit(RXRPC_CALL_DISCONNECTED, &call->flags))
338                 return -ECONNRESET;
339
340         conn = call->conn;
341
342         msg.msg_name    = &call->peer->srx.transport;
343         msg.msg_namelen = call->peer->srx.transport_len;
344         msg.msg_control = NULL;
345         msg.msg_controllen = 0;
346         msg.msg_flags   = 0;
347
348         pkt.whdr.epoch          = htonl(conn->proto.epoch);
349         pkt.whdr.cid            = htonl(call->cid);
350         pkt.whdr.callNumber     = htonl(call->call_id);
351         pkt.whdr.seq            = 0;
352         pkt.whdr.type           = RXRPC_PACKET_TYPE_ABORT;
353         pkt.whdr.flags          = conn->out_clientflag;
354         pkt.whdr.userStatus     = 0;
355         pkt.whdr.securityIndex  = call->security_ix;
356         pkt.whdr._rsvd          = 0;
357         pkt.whdr.serviceId      = htons(call->service_id);
358         pkt.abort_code          = htonl(call->abort_code);
359
360         iov[0].iov_base = &pkt;
361         iov[0].iov_len  = sizeof(pkt);
362
363         serial = atomic_inc_return(&conn->serial);
364         pkt.whdr.serial = htonl(serial);
365
366         iov_iter_kvec(&msg.msg_iter, WRITE, iov, 1, sizeof(pkt));
367         ret = do_udp_sendmsg(conn->params.local->socket, &msg, sizeof(pkt));
368         conn->params.peer->last_tx_at = ktime_get_seconds();
369         if (ret < 0)
370                 trace_rxrpc_tx_fail(call->debug_id, serial, ret,
371                                     rxrpc_tx_point_call_abort);
372         else
373                 trace_rxrpc_tx_packet(call->debug_id, &pkt.whdr,
374                                       rxrpc_tx_point_call_abort);
375         rxrpc_tx_backoff(call, ret);
376         return ret;
377 }
378
379 /*
380  * send a packet through the transport endpoint
381  */
382 int rxrpc_send_data_packet(struct rxrpc_call *call, struct rxrpc_txbuf *txb)
383 {
384         enum rxrpc_req_ack_trace why;
385         struct rxrpc_connection *conn = call->conn;
386         struct msghdr msg;
387         struct kvec iov[1];
388         rxrpc_serial_t serial;
389         size_t len;
390         int ret, rtt_slot = -1;
391
392         _enter("%x,{%d}", txb->seq, txb->len);
393
394         if (hlist_unhashed(&call->error_link)) {
395                 spin_lock_bh(&call->peer->lock);
396                 hlist_add_head_rcu(&call->error_link, &call->peer->error_targets);
397                 spin_unlock_bh(&call->peer->lock);
398         }
399
400         /* Each transmission of a Tx packet needs a new serial number */
401         serial = atomic_inc_return(&conn->serial);
402         txb->wire.serial = htonl(serial);
403
404         if (test_bit(RXRPC_CONN_PROBING_FOR_UPGRADE, &conn->flags) &&
405             txb->seq == 1)
406                 txb->wire.userStatus = RXRPC_USERSTATUS_SERVICE_UPGRADE;
407
408         iov[0].iov_base = &txb->wire;
409         iov[0].iov_len = sizeof(txb->wire) + txb->len;
410         len = iov[0].iov_len;
411         iov_iter_kvec(&msg.msg_iter, WRITE, iov, 1, len);
412
413         msg.msg_name = &call->peer->srx.transport;
414         msg.msg_namelen = call->peer->srx.transport_len;
415         msg.msg_control = NULL;
416         msg.msg_controllen = 0;
417         msg.msg_flags = 0;
418
419         /* If our RTT cache needs working on, request an ACK.  Also request
420          * ACKs if a DATA packet appears to have been lost.
421          *
422          * However, we mustn't request an ACK on the last reply packet of a
423          * service call, lest OpenAFS incorrectly send us an ACK with some
424          * soft-ACKs in it and then never follow up with a proper hard ACK.
425          */
426         if (txb->wire.flags & RXRPC_REQUEST_ACK)
427                 why = rxrpc_reqack_already_on;
428         else if (test_bit(RXRPC_TXBUF_LAST, &txb->flags) && rxrpc_sending_to_client(txb))
429                 why = rxrpc_reqack_no_srv_last;
430         else if (test_and_clear_bit(RXRPC_CALL_EV_ACK_LOST, &call->events))
431                 why = rxrpc_reqack_ack_lost;
432         else if (test_bit(RXRPC_TXBUF_RESENT, &txb->flags))
433                 why = rxrpc_reqack_retrans;
434         else if (call->cong_mode == RXRPC_CALL_SLOW_START && call->cong_cwnd <= 2)
435                 why = rxrpc_reqack_slow_start;
436         else if (call->tx_winsize <= 2)
437                 why = rxrpc_reqack_small_txwin;
438         else if (call->peer->rtt_count < 3 && txb->seq & 1)
439                 why = rxrpc_reqack_more_rtt;
440         else if (ktime_before(ktime_add_ms(call->peer->rtt_last_req, 1000), ktime_get_real()))
441                 why = rxrpc_reqack_old_rtt;
442         else
443                 goto dont_set_request_ack;
444
445         rxrpc_inc_stat(call->rxnet, stat_why_req_ack[why]);
446         trace_rxrpc_req_ack(call->debug_id, txb->seq, why);
447         if (why != rxrpc_reqack_no_srv_last)
448                 txb->wire.flags |= RXRPC_REQUEST_ACK;
449 dont_set_request_ack:
450
451         if (IS_ENABLED(CONFIG_AF_RXRPC_INJECT_LOSS)) {
452                 static int lose;
453                 if ((lose++ & 7) == 7) {
454                         ret = 0;
455                         trace_rxrpc_tx_data(call, txb->seq, serial,
456                                             txb->wire.flags,
457                                             test_bit(RXRPC_TXBUF_RESENT, &txb->flags),
458                                             true);
459                         goto done;
460                 }
461         }
462
463         trace_rxrpc_tx_data(call, txb->seq, serial, txb->wire.flags,
464                             test_bit(RXRPC_TXBUF_RESENT, &txb->flags), false);
465         cmpxchg(&call->tx_transmitted, txb->seq - 1, txb->seq);
466
467         /* send the packet with the don't fragment bit set if we currently
468          * think it's small enough */
469         if (txb->len >= call->peer->maxdata)
470                 goto send_fragmentable;
471
472         down_read(&conn->params.local->defrag_sem);
473
474         txb->last_sent = ktime_get_real();
475         if (txb->wire.flags & RXRPC_REQUEST_ACK)
476                 rtt_slot = rxrpc_begin_rtt_probe(call, serial, rxrpc_rtt_tx_data);
477
478         /* send the packet by UDP
479          * - returns -EMSGSIZE if UDP would have to fragment the packet
480          *   to go out of the interface
481          *   - in which case, we'll have processed the ICMP error
482          *     message and update the peer record
483          */
484         rxrpc_inc_stat(call->rxnet, stat_tx_data_send);
485         ret = do_udp_sendmsg(conn->params.local->socket, &msg, len);
486         conn->params.peer->last_tx_at = ktime_get_seconds();
487
488         up_read(&conn->params.local->defrag_sem);
489         if (ret < 0) {
490                 rxrpc_cancel_rtt_probe(call, serial, rtt_slot);
491                 trace_rxrpc_tx_fail(call->debug_id, serial, ret,
492                                     rxrpc_tx_point_call_data_nofrag);
493         } else {
494                 trace_rxrpc_tx_packet(call->debug_id, &txb->wire,
495                                       rxrpc_tx_point_call_data_nofrag);
496         }
497
498         rxrpc_tx_backoff(call, ret);
499         if (ret == -EMSGSIZE)
500                 goto send_fragmentable;
501
502 done:
503         if (ret >= 0) {
504                 call->tx_last_sent = txb->last_sent;
505                 if (txb->wire.flags & RXRPC_REQUEST_ACK) {
506                         call->peer->rtt_last_req = txb->last_sent;
507                         if (call->peer->rtt_count > 1) {
508                                 unsigned long nowj = jiffies, ack_lost_at;
509
510                                 ack_lost_at = rxrpc_get_rto_backoff(call->peer, false);
511                                 ack_lost_at += nowj;
512                                 WRITE_ONCE(call->ack_lost_at, ack_lost_at);
513                                 rxrpc_reduce_call_timer(call, ack_lost_at, nowj,
514                                                         rxrpc_timer_set_for_lost_ack);
515                         }
516                 }
517
518                 if (txb->seq == 1 &&
519                     !test_and_set_bit(RXRPC_CALL_BEGAN_RX_TIMER,
520                                       &call->flags)) {
521                         unsigned long nowj = jiffies, expect_rx_by;
522
523                         expect_rx_by = nowj + call->next_rx_timo;
524                         WRITE_ONCE(call->expect_rx_by, expect_rx_by);
525                         rxrpc_reduce_call_timer(call, expect_rx_by, nowj,
526                                                 rxrpc_timer_set_for_normal);
527                 }
528
529                 rxrpc_set_keepalive(call);
530         } else {
531                 /* Cancel the call if the initial transmission fails,
532                  * particularly if that's due to network routing issues that
533                  * aren't going away anytime soon.  The layer above can arrange
534                  * the retransmission.
535                  */
536                 if (!test_and_set_bit(RXRPC_CALL_BEGAN_RX_TIMER, &call->flags))
537                         rxrpc_set_call_completion(call, RXRPC_CALL_LOCAL_ERROR,
538                                                   RX_USER_ABORT, ret);
539         }
540
541         _leave(" = %d [%u]", ret, call->peer->maxdata);
542         return ret;
543
544 send_fragmentable:
545         /* attempt to send this message with fragmentation enabled */
546         _debug("send fragment");
547
548         down_write(&conn->params.local->defrag_sem);
549
550         txb->last_sent = ktime_get_real();
551         if (txb->wire.flags & RXRPC_REQUEST_ACK)
552                 rtt_slot = rxrpc_begin_rtt_probe(call, serial, rxrpc_rtt_tx_data);
553
554         switch (conn->params.local->srx.transport.family) {
555         case AF_INET6:
556         case AF_INET:
557                 ip_sock_set_mtu_discover(conn->params.local->socket->sk,
558                                          IP_PMTUDISC_DONT);
559                 rxrpc_inc_stat(call->rxnet, stat_tx_data_send_frag);
560                 ret = do_udp_sendmsg(conn->params.local->socket, &msg, len);
561                 conn->params.peer->last_tx_at = ktime_get_seconds();
562
563                 ip_sock_set_mtu_discover(conn->params.local->socket->sk,
564                                          IP_PMTUDISC_DO);
565                 break;
566
567         default:
568                 BUG();
569         }
570
571         if (ret < 0) {
572                 rxrpc_cancel_rtt_probe(call, serial, rtt_slot);
573                 trace_rxrpc_tx_fail(call->debug_id, serial, ret,
574                                     rxrpc_tx_point_call_data_frag);
575         } else {
576                 trace_rxrpc_tx_packet(call->debug_id, &txb->wire,
577                                       rxrpc_tx_point_call_data_frag);
578         }
579         rxrpc_tx_backoff(call, ret);
580
581         up_write(&conn->params.local->defrag_sem);
582         goto done;
583 }
584
585 /*
586  * reject packets through the local endpoint
587  */
588 void rxrpc_reject_packets(struct rxrpc_local *local)
589 {
590         struct sockaddr_rxrpc srx;
591         struct rxrpc_skb_priv *sp;
592         struct rxrpc_wire_header whdr;
593         struct sk_buff *skb;
594         struct msghdr msg;
595         struct kvec iov[2];
596         size_t size;
597         __be32 code;
598         int ret, ioc;
599
600         _enter("%d", local->debug_id);
601
602         iov[0].iov_base = &whdr;
603         iov[0].iov_len = sizeof(whdr);
604         iov[1].iov_base = &code;
605         iov[1].iov_len = sizeof(code);
606
607         msg.msg_name = &srx.transport;
608         msg.msg_control = NULL;
609         msg.msg_controllen = 0;
610         msg.msg_flags = 0;
611
612         memset(&whdr, 0, sizeof(whdr));
613
614         while ((skb = skb_dequeue(&local->reject_queue))) {
615                 rxrpc_see_skb(skb, rxrpc_skb_seen);
616                 sp = rxrpc_skb(skb);
617
618                 switch (skb->mark) {
619                 case RXRPC_SKB_MARK_REJECT_BUSY:
620                         whdr.type = RXRPC_PACKET_TYPE_BUSY;
621                         size = sizeof(whdr);
622                         ioc = 1;
623                         break;
624                 case RXRPC_SKB_MARK_REJECT_ABORT:
625                         whdr.type = RXRPC_PACKET_TYPE_ABORT;
626                         code = htonl(skb->priority);
627                         size = sizeof(whdr) + sizeof(code);
628                         ioc = 2;
629                         break;
630                 default:
631                         rxrpc_free_skb(skb, rxrpc_skb_freed);
632                         continue;
633                 }
634
635                 if (rxrpc_extract_addr_from_skb(&srx, skb) == 0) {
636                         msg.msg_namelen = srx.transport_len;
637
638                         whdr.epoch      = htonl(sp->hdr.epoch);
639                         whdr.cid        = htonl(sp->hdr.cid);
640                         whdr.callNumber = htonl(sp->hdr.callNumber);
641                         whdr.serviceId  = htons(sp->hdr.serviceId);
642                         whdr.flags      = sp->hdr.flags;
643                         whdr.flags      ^= RXRPC_CLIENT_INITIATED;
644                         whdr.flags      &= RXRPC_CLIENT_INITIATED;
645
646                         iov_iter_kvec(&msg.msg_iter, WRITE, iov, ioc, size);
647                         ret = do_udp_sendmsg(local->socket, &msg, size);
648                         if (ret < 0)
649                                 trace_rxrpc_tx_fail(local->debug_id, 0, ret,
650                                                     rxrpc_tx_point_reject);
651                         else
652                                 trace_rxrpc_tx_packet(local->debug_id, &whdr,
653                                                       rxrpc_tx_point_reject);
654                 }
655
656                 rxrpc_free_skb(skb, rxrpc_skb_freed);
657         }
658
659         _leave("");
660 }
661
662 /*
663  * Send a VERSION reply to a peer as a keepalive.
664  */
665 void rxrpc_send_keepalive(struct rxrpc_peer *peer)
666 {
667         struct rxrpc_wire_header whdr;
668         struct msghdr msg;
669         struct kvec iov[2];
670         size_t len;
671         int ret;
672
673         _enter("");
674
675         msg.msg_name    = &peer->srx.transport;
676         msg.msg_namelen = peer->srx.transport_len;
677         msg.msg_control = NULL;
678         msg.msg_controllen = 0;
679         msg.msg_flags   = 0;
680
681         whdr.epoch      = htonl(peer->local->rxnet->epoch);
682         whdr.cid        = 0;
683         whdr.callNumber = 0;
684         whdr.seq        = 0;
685         whdr.serial     = 0;
686         whdr.type       = RXRPC_PACKET_TYPE_VERSION; /* Not client-initiated */
687         whdr.flags      = RXRPC_LAST_PACKET;
688         whdr.userStatus = 0;
689         whdr.securityIndex = 0;
690         whdr._rsvd      = 0;
691         whdr.serviceId  = 0;
692
693         iov[0].iov_base = &whdr;
694         iov[0].iov_len  = sizeof(whdr);
695         iov[1].iov_base = (char *)rxrpc_keepalive_string;
696         iov[1].iov_len  = sizeof(rxrpc_keepalive_string);
697
698         len = iov[0].iov_len + iov[1].iov_len;
699
700         _proto("Tx VERSION (keepalive)");
701
702         iov_iter_kvec(&msg.msg_iter, WRITE, iov, 2, len);
703         ret = do_udp_sendmsg(peer->local->socket, &msg, len);
704         if (ret < 0)
705                 trace_rxrpc_tx_fail(peer->debug_id, 0, ret,
706                                     rxrpc_tx_point_version_keepalive);
707         else
708                 trace_rxrpc_tx_packet(peer->debug_id, &whdr,
709                                       rxrpc_tx_point_version_keepalive);
710
711         peer->last_tx_at = ktime_get_seconds();
712         _leave("");
713 }