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