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