rxrpc: Remove call->lock
[platform/kernel/linux-rpi.git] / net / rxrpc / input.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* RxRPC packet reception
3  *
4  * Copyright (C) 2007, 2016 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 "ar-internal.h"
11
12 static void rxrpc_proto_abort(const char *why,
13                               struct rxrpc_call *call, rxrpc_seq_t seq)
14 {
15         if (rxrpc_abort_call(why, call, seq, RX_PROTOCOL_ERROR, -EBADMSG)) {
16                 set_bit(RXRPC_CALL_EV_ABORT, &call->events);
17                 rxrpc_queue_call(call);
18         }
19 }
20
21 /*
22  * Do TCP-style congestion management [RFC 5681].
23  */
24 static void rxrpc_congestion_management(struct rxrpc_call *call,
25                                         struct sk_buff *skb,
26                                         struct rxrpc_ack_summary *summary,
27                                         rxrpc_serial_t acked_serial)
28 {
29         enum rxrpc_congest_change change = rxrpc_cong_no_change;
30         unsigned int cumulative_acks = call->cong_cumul_acks;
31         unsigned int cwnd = call->cong_cwnd;
32         bool resend = false;
33
34         summary->flight_size =
35                 (call->tx_top - call->acks_hard_ack) - summary->nr_acks;
36
37         if (test_and_clear_bit(RXRPC_CALL_RETRANS_TIMEOUT, &call->flags)) {
38                 summary->retrans_timeo = true;
39                 call->cong_ssthresh = max_t(unsigned int,
40                                             summary->flight_size / 2, 2);
41                 cwnd = 1;
42                 if (cwnd >= call->cong_ssthresh &&
43                     call->cong_mode == RXRPC_CALL_SLOW_START) {
44                         call->cong_mode = RXRPC_CALL_CONGEST_AVOIDANCE;
45                         call->cong_tstamp = skb->tstamp;
46                         cumulative_acks = 0;
47                 }
48         }
49
50         cumulative_acks += summary->nr_new_acks;
51         cumulative_acks += summary->nr_rot_new_acks;
52         if (cumulative_acks > 255)
53                 cumulative_acks = 255;
54
55         summary->mode = call->cong_mode;
56         summary->cwnd = call->cong_cwnd;
57         summary->ssthresh = call->cong_ssthresh;
58         summary->cumulative_acks = cumulative_acks;
59         summary->dup_acks = call->cong_dup_acks;
60
61         switch (call->cong_mode) {
62         case RXRPC_CALL_SLOW_START:
63                 if (summary->nr_nacks > 0)
64                         goto packet_loss_detected;
65                 if (summary->cumulative_acks > 0)
66                         cwnd += 1;
67                 if (cwnd >= call->cong_ssthresh) {
68                         call->cong_mode = RXRPC_CALL_CONGEST_AVOIDANCE;
69                         call->cong_tstamp = skb->tstamp;
70                 }
71                 goto out;
72
73         case RXRPC_CALL_CONGEST_AVOIDANCE:
74                 if (summary->nr_nacks > 0)
75                         goto packet_loss_detected;
76
77                 /* We analyse the number of packets that get ACK'd per RTT
78                  * period and increase the window if we managed to fill it.
79                  */
80                 if (call->peer->rtt_count == 0)
81                         goto out;
82                 if (ktime_before(skb->tstamp,
83                                  ktime_add_us(call->cong_tstamp,
84                                               call->peer->srtt_us >> 3)))
85                         goto out_no_clear_ca;
86                 change = rxrpc_cong_rtt_window_end;
87                 call->cong_tstamp = skb->tstamp;
88                 if (cumulative_acks >= cwnd)
89                         cwnd++;
90                 goto out;
91
92         case RXRPC_CALL_PACKET_LOSS:
93                 if (summary->nr_nacks == 0)
94                         goto resume_normality;
95
96                 if (summary->new_low_nack) {
97                         change = rxrpc_cong_new_low_nack;
98                         call->cong_dup_acks = 1;
99                         if (call->cong_extra > 1)
100                                 call->cong_extra = 1;
101                         goto send_extra_data;
102                 }
103
104                 call->cong_dup_acks++;
105                 if (call->cong_dup_acks < 3)
106                         goto send_extra_data;
107
108                 change = rxrpc_cong_begin_retransmission;
109                 call->cong_mode = RXRPC_CALL_FAST_RETRANSMIT;
110                 call->cong_ssthresh = max_t(unsigned int,
111                                             summary->flight_size / 2, 2);
112                 cwnd = call->cong_ssthresh + 3;
113                 call->cong_extra = 0;
114                 call->cong_dup_acks = 0;
115                 resend = true;
116                 goto out;
117
118         case RXRPC_CALL_FAST_RETRANSMIT:
119                 if (!summary->new_low_nack) {
120                         if (summary->nr_new_acks == 0)
121                                 cwnd += 1;
122                         call->cong_dup_acks++;
123                         if (call->cong_dup_acks == 2) {
124                                 change = rxrpc_cong_retransmit_again;
125                                 call->cong_dup_acks = 0;
126                                 resend = true;
127                         }
128                 } else {
129                         change = rxrpc_cong_progress;
130                         cwnd = call->cong_ssthresh;
131                         if (summary->nr_nacks == 0)
132                                 goto resume_normality;
133                 }
134                 goto out;
135
136         default:
137                 BUG();
138                 goto out;
139         }
140
141 resume_normality:
142         change = rxrpc_cong_cleared_nacks;
143         call->cong_dup_acks = 0;
144         call->cong_extra = 0;
145         call->cong_tstamp = skb->tstamp;
146         if (cwnd < call->cong_ssthresh)
147                 call->cong_mode = RXRPC_CALL_SLOW_START;
148         else
149                 call->cong_mode = RXRPC_CALL_CONGEST_AVOIDANCE;
150 out:
151         cumulative_acks = 0;
152 out_no_clear_ca:
153         if (cwnd >= RXRPC_TX_MAX_WINDOW)
154                 cwnd = RXRPC_TX_MAX_WINDOW;
155         call->cong_cwnd = cwnd;
156         call->cong_cumul_acks = cumulative_acks;
157         trace_rxrpc_congest(call, summary, acked_serial, change);
158         if (resend && !test_and_set_bit(RXRPC_CALL_EV_RESEND, &call->events))
159                 rxrpc_queue_call(call);
160         return;
161
162 packet_loss_detected:
163         change = rxrpc_cong_saw_nack;
164         call->cong_mode = RXRPC_CALL_PACKET_LOSS;
165         call->cong_dup_acks = 0;
166         goto send_extra_data;
167
168 send_extra_data:
169         /* Send some previously unsent DATA if we have some to advance the ACK
170          * state.
171          */
172         if (test_bit(RXRPC_CALL_TX_LAST, &call->flags) ||
173             summary->nr_acks != call->tx_top - call->acks_hard_ack) {
174                 call->cong_extra++;
175                 wake_up(&call->waitq);
176         }
177         goto out_no_clear_ca;
178 }
179
180 /*
181  * Apply a hard ACK by advancing the Tx window.
182  */
183 static bool rxrpc_rotate_tx_window(struct rxrpc_call *call, rxrpc_seq_t to,
184                                    struct rxrpc_ack_summary *summary)
185 {
186         struct rxrpc_txbuf *txb;
187         bool rot_last = false;
188
189         list_for_each_entry_rcu(txb, &call->tx_buffer, call_link, false) {
190                 if (before_eq(txb->seq, call->acks_hard_ack))
191                         continue;
192                 if (!test_bit(RXRPC_TXBUF_ACKED, &txb->flags))
193                         summary->nr_rot_new_acks++;
194                 if (test_bit(RXRPC_TXBUF_LAST, &txb->flags)) {
195                         set_bit(RXRPC_CALL_TX_LAST, &call->flags);
196                         rot_last = true;
197                 }
198                 if (txb->seq == to)
199                         break;
200         }
201
202         if (rot_last)
203                 set_bit(RXRPC_CALL_TX_ALL_ACKED, &call->flags);
204
205         _enter("%x,%x,%x,%d", to, call->acks_hard_ack, call->tx_top, rot_last);
206
207         if (call->acks_lowest_nak == call->acks_hard_ack) {
208                 call->acks_lowest_nak = to;
209         } else if (before_eq(call->acks_lowest_nak, to)) {
210                 summary->new_low_nack = true;
211                 call->acks_lowest_nak = to;
212         }
213
214         smp_store_release(&call->acks_hard_ack, to);
215
216         trace_rxrpc_txqueue(call, (rot_last ?
217                                    rxrpc_txqueue_rotate_last :
218                                    rxrpc_txqueue_rotate));
219         wake_up(&call->waitq);
220         return rot_last;
221 }
222
223 /*
224  * End the transmission phase of a call.
225  *
226  * This occurs when we get an ACKALL packet, the first DATA packet of a reply,
227  * or a final ACK packet.
228  */
229 static bool rxrpc_end_tx_phase(struct rxrpc_call *call, bool reply_begun,
230                                const char *abort_why)
231 {
232         unsigned int state;
233
234         ASSERT(test_bit(RXRPC_CALL_TX_LAST, &call->flags));
235
236         write_lock(&call->state_lock);
237
238         state = call->state;
239         switch (state) {
240         case RXRPC_CALL_CLIENT_SEND_REQUEST:
241         case RXRPC_CALL_CLIENT_AWAIT_REPLY:
242                 if (reply_begun)
243                         call->state = state = RXRPC_CALL_CLIENT_RECV_REPLY;
244                 else
245                         call->state = state = RXRPC_CALL_CLIENT_AWAIT_REPLY;
246                 break;
247
248         case RXRPC_CALL_SERVER_AWAIT_ACK:
249                 __rxrpc_call_completed(call);
250                 state = call->state;
251                 break;
252
253         default:
254                 goto bad_state;
255         }
256
257         write_unlock(&call->state_lock);
258         if (state == RXRPC_CALL_CLIENT_AWAIT_REPLY)
259                 trace_rxrpc_txqueue(call, rxrpc_txqueue_await_reply);
260         else
261                 trace_rxrpc_txqueue(call, rxrpc_txqueue_end);
262         _leave(" = ok");
263         return true;
264
265 bad_state:
266         write_unlock(&call->state_lock);
267         kdebug("end_tx %s", rxrpc_call_states[call->state]);
268         rxrpc_proto_abort(abort_why, call, call->tx_top);
269         return false;
270 }
271
272 /*
273  * Begin the reply reception phase of a call.
274  */
275 static bool rxrpc_receiving_reply(struct rxrpc_call *call)
276 {
277         struct rxrpc_ack_summary summary = { 0 };
278         unsigned long now, timo;
279         rxrpc_seq_t top = READ_ONCE(call->tx_top);
280
281         if (call->ackr_reason) {
282                 now = jiffies;
283                 timo = now + MAX_JIFFY_OFFSET;
284                 WRITE_ONCE(call->resend_at, timo);
285                 WRITE_ONCE(call->delay_ack_at, timo);
286                 trace_rxrpc_timer(call, rxrpc_timer_init_for_reply, now);
287         }
288
289         if (!test_bit(RXRPC_CALL_TX_LAST, &call->flags)) {
290                 if (!rxrpc_rotate_tx_window(call, top, &summary)) {
291                         rxrpc_proto_abort("TXL", call, top);
292                         return false;
293                 }
294         }
295         return rxrpc_end_tx_phase(call, true, "ETD");
296 }
297
298 static void rxrpc_input_update_ack_window(struct rxrpc_call *call,
299                                           rxrpc_seq_t window, rxrpc_seq_t wtop)
300 {
301         atomic64_set_release(&call->ackr_window, ((u64)wtop) << 32 | window);
302 }
303
304 /*
305  * Push a DATA packet onto the Rx queue.
306  */
307 static void rxrpc_input_queue_data(struct rxrpc_call *call, struct sk_buff *skb,
308                                    rxrpc_seq_t window, rxrpc_seq_t wtop,
309                                    enum rxrpc_receive_trace why)
310 {
311         struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
312         bool last = sp->hdr.flags & RXRPC_LAST_PACKET;
313
314         __skb_queue_tail(&call->recvmsg_queue, skb);
315         rxrpc_input_update_ack_window(call, window, wtop);
316
317         trace_rxrpc_receive(call, last ? why + 1 : why, sp->hdr.serial, sp->hdr.seq);
318 }
319
320 /*
321  * Process a DATA packet.
322  */
323 static void rxrpc_input_data_one(struct rxrpc_call *call, struct sk_buff *skb)
324 {
325         struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
326         struct sk_buff *oos;
327         rxrpc_serial_t serial = sp->hdr.serial;
328         u64 win = atomic64_read(&call->ackr_window);
329         rxrpc_seq_t window = lower_32_bits(win);
330         rxrpc_seq_t wtop = upper_32_bits(win);
331         rxrpc_seq_t wlimit = window + call->rx_winsize - 1;
332         rxrpc_seq_t seq = sp->hdr.seq;
333         bool last = sp->hdr.flags & RXRPC_LAST_PACKET;
334         int ack_reason = -1;
335
336         rxrpc_inc_stat(call->rxnet, stat_rx_data);
337         if (sp->hdr.flags & RXRPC_REQUEST_ACK)
338                 rxrpc_inc_stat(call->rxnet, stat_rx_data_reqack);
339         if (sp->hdr.flags & RXRPC_JUMBO_PACKET)
340                 rxrpc_inc_stat(call->rxnet, stat_rx_data_jumbo);
341
342         if (last) {
343                 if (test_and_set_bit(RXRPC_CALL_RX_LAST, &call->flags) &&
344                     seq + 1 != wtop) {
345                         rxrpc_proto_abort("LSN", call, seq);
346                         goto err_free;
347                 }
348         } else {
349                 if (test_bit(RXRPC_CALL_RX_LAST, &call->flags) &&
350                     after_eq(seq, wtop)) {
351                         pr_warn("Packet beyond last: c=%x q=%x window=%x-%x wlimit=%x\n",
352                                 call->debug_id, seq, window, wtop, wlimit);
353                         rxrpc_proto_abort("LSA", call, seq);
354                         goto err_free;
355                 }
356         }
357
358         if (after(seq, call->rx_highest_seq))
359                 call->rx_highest_seq = seq;
360
361         trace_rxrpc_rx_data(call->debug_id, seq, serial, sp->hdr.flags);
362
363         if (before(seq, window)) {
364                 ack_reason = RXRPC_ACK_DUPLICATE;
365                 goto send_ack;
366         }
367         if (after(seq, wlimit)) {
368                 ack_reason = RXRPC_ACK_EXCEEDS_WINDOW;
369                 goto send_ack;
370         }
371
372         /* Queue the packet. */
373         if (seq == window) {
374                 rxrpc_seq_t reset_from;
375                 bool reset_sack = false;
376
377                 if (sp->hdr.flags & RXRPC_REQUEST_ACK)
378                         ack_reason = RXRPC_ACK_REQUESTED;
379                 /* Send an immediate ACK if we fill in a hole */
380                 else if (!skb_queue_empty(&call->rx_oos_queue))
381                         ack_reason = RXRPC_ACK_DELAY;
382
383                 window++;
384                 if (after(window, wtop))
385                         wtop = window;
386
387                 spin_lock(&call->recvmsg_queue.lock);
388                 rxrpc_input_queue_data(call, skb, window, wtop, rxrpc_receive_queue);
389                 skb = NULL;
390
391                 while ((oos = skb_peek(&call->rx_oos_queue))) {
392                         struct rxrpc_skb_priv *osp = rxrpc_skb(oos);
393
394                         if (after(osp->hdr.seq, window))
395                                 break;
396
397                         __skb_unlink(oos, &call->rx_oos_queue);
398                         last = osp->hdr.flags & RXRPC_LAST_PACKET;
399                         seq = osp->hdr.seq;
400                         if (!reset_sack) {
401                                 reset_from = seq;
402                                 reset_sack = true;
403                         }
404
405                         window++;
406                         rxrpc_input_queue_data(call, oos, window, wtop,
407                                                  rxrpc_receive_queue_oos);
408                 }
409
410                 spin_unlock(&call->recvmsg_queue.lock);
411
412                 if (reset_sack) {
413                         do {
414                                 call->ackr_sack_table[reset_from % RXRPC_SACK_SIZE] = 0;
415                         } while (reset_from++, before(reset_from, window));
416                 }
417         } else {
418                 bool keep = false;
419
420                 ack_reason = RXRPC_ACK_OUT_OF_SEQUENCE;
421
422                 if (!call->ackr_sack_table[seq % RXRPC_SACK_SIZE]) {
423                         call->ackr_sack_table[seq % RXRPC_SACK_SIZE] = 1;
424                         keep = 1;
425                 }
426
427                 if (after(seq + 1, wtop)) {
428                         wtop = seq + 1;
429                         rxrpc_input_update_ack_window(call, window, wtop);
430                 }
431
432                 if (!keep) {
433                         ack_reason = RXRPC_ACK_DUPLICATE;
434                         goto send_ack;
435                 }
436
437                 skb_queue_walk(&call->rx_oos_queue, oos) {
438                         struct rxrpc_skb_priv *osp = rxrpc_skb(oos);
439
440                         if (after(osp->hdr.seq, seq)) {
441                                 __skb_queue_before(&call->rx_oos_queue, oos, skb);
442                                 goto oos_queued;
443                         }
444                 }
445
446                 __skb_queue_tail(&call->rx_oos_queue, skb);
447         oos_queued:
448                 trace_rxrpc_receive(call, last ? rxrpc_receive_oos_last : rxrpc_receive_oos,
449                                     sp->hdr.serial, sp->hdr.seq);
450                 skb = NULL;
451         }
452
453 send_ack:
454         if (ack_reason < 0 &&
455             atomic_inc_return(&call->ackr_nr_unacked) > 2 &&
456             test_and_set_bit(RXRPC_CALL_IDLE_ACK_PENDING, &call->flags)) {
457                 ack_reason = RXRPC_ACK_IDLE;
458         } else if (ack_reason >= 0) {
459                 set_bit(RXRPC_CALL_IDLE_ACK_PENDING, &call->flags);
460         }
461
462         if (ack_reason >= 0)
463                 rxrpc_send_ACK(call, ack_reason, serial,
464                                rxrpc_propose_ack_input_data);
465         else
466                 rxrpc_propose_delay_ACK(call, serial,
467                                         rxrpc_propose_ack_input_data);
468
469 err_free:
470         rxrpc_free_skb(skb, rxrpc_skb_freed);
471 }
472
473 /*
474  * Split a jumbo packet and file the bits separately.
475  */
476 static bool rxrpc_input_split_jumbo(struct rxrpc_call *call, struct sk_buff *skb)
477 {
478         struct rxrpc_jumbo_header jhdr;
479         struct rxrpc_skb_priv *sp = rxrpc_skb(skb), *jsp;
480         struct sk_buff *jskb;
481         unsigned int offset = sizeof(struct rxrpc_wire_header);
482         unsigned int len = skb->len - offset;
483
484         while (sp->hdr.flags & RXRPC_JUMBO_PACKET) {
485                 if (len < RXRPC_JUMBO_SUBPKTLEN)
486                         goto protocol_error;
487                 if (sp->hdr.flags & RXRPC_LAST_PACKET)
488                         goto protocol_error;
489                 if (skb_copy_bits(skb, offset + RXRPC_JUMBO_DATALEN,
490                                   &jhdr, sizeof(jhdr)) < 0)
491                         goto protocol_error;
492
493                 jskb = skb_clone(skb, GFP_ATOMIC);
494                 if (!jskb) {
495                         kdebug("couldn't clone");
496                         return false;
497                 }
498                 rxrpc_new_skb(jskb, rxrpc_skb_cloned_jumbo);
499                 jsp = rxrpc_skb(jskb);
500                 jsp->offset = offset;
501                 jsp->len = RXRPC_JUMBO_DATALEN;
502                 rxrpc_input_data_one(call, jskb);
503
504                 sp->hdr.flags = jhdr.flags;
505                 sp->hdr._rsvd = ntohs(jhdr._rsvd);
506                 sp->hdr.seq++;
507                 sp->hdr.serial++;
508                 offset += RXRPC_JUMBO_SUBPKTLEN;
509                 len -= RXRPC_JUMBO_SUBPKTLEN;
510         }
511
512         sp->offset = offset;
513         sp->len    = len;
514         rxrpc_input_data_one(call, skb);
515         return true;
516
517 protocol_error:
518         return false;
519 }
520
521 /*
522  * Process a DATA packet, adding the packet to the Rx ring.  The caller's
523  * packet ref must be passed on or discarded.
524  */
525 static void rxrpc_input_data(struct rxrpc_call *call, struct sk_buff *skb)
526 {
527         struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
528         enum rxrpc_call_state state;
529         rxrpc_serial_t serial = sp->hdr.serial;
530         rxrpc_seq_t seq0 = sp->hdr.seq;
531
532         _enter("{%llx,%x},{%u,%x}",
533                atomic64_read(&call->ackr_window), call->rx_highest_seq,
534                skb->len, seq0);
535
536         _proto("Rx DATA %%%u { #%u f=%02x }",
537                sp->hdr.serial, seq0, sp->hdr.flags);
538
539         state = READ_ONCE(call->state);
540         if (state >= RXRPC_CALL_COMPLETE) {
541                 rxrpc_free_skb(skb, rxrpc_skb_freed);
542                 return;
543         }
544
545         /* Unshare the packet so that it can be modified for in-place
546          * decryption.
547          */
548         if (sp->hdr.securityIndex != 0) {
549                 struct sk_buff *nskb = skb_unshare(skb, GFP_ATOMIC);
550                 if (!nskb) {
551                         rxrpc_eaten_skb(skb, rxrpc_skb_unshared_nomem);
552                         return;
553                 }
554
555                 if (nskb != skb) {
556                         rxrpc_eaten_skb(skb, rxrpc_skb_received);
557                         skb = nskb;
558                         rxrpc_new_skb(skb, rxrpc_skb_unshared);
559                         sp = rxrpc_skb(skb);
560                 }
561         }
562
563         if (state == RXRPC_CALL_SERVER_RECV_REQUEST) {
564                 unsigned long timo = READ_ONCE(call->next_req_timo);
565                 unsigned long now, expect_req_by;
566
567                 if (timo) {
568                         now = jiffies;
569                         expect_req_by = now + timo;
570                         WRITE_ONCE(call->expect_req_by, expect_req_by);
571                         rxrpc_reduce_call_timer(call, expect_req_by, now,
572                                                 rxrpc_timer_set_for_idle);
573                 }
574         }
575
576         spin_lock(&call->input_lock);
577
578         /* Received data implicitly ACKs all of the request packets we sent
579          * when we're acting as a client.
580          */
581         if ((state == RXRPC_CALL_CLIENT_SEND_REQUEST ||
582              state == RXRPC_CALL_CLIENT_AWAIT_REPLY) &&
583             !rxrpc_receiving_reply(call))
584                 goto out;
585
586         if (!rxrpc_input_split_jumbo(call, skb)) {
587                 rxrpc_proto_abort("VLD", call, sp->hdr.seq);
588                 goto out;
589         }
590         skb = NULL;
591
592 out:
593         trace_rxrpc_notify_socket(call->debug_id, serial);
594         rxrpc_notify_socket(call);
595
596         spin_unlock(&call->input_lock);
597         rxrpc_free_skb(skb, rxrpc_skb_freed);
598         _leave(" [queued]");
599 }
600
601 /*
602  * See if there's a cached RTT probe to complete.
603  */
604 static void rxrpc_complete_rtt_probe(struct rxrpc_call *call,
605                                      ktime_t resp_time,
606                                      rxrpc_serial_t acked_serial,
607                                      rxrpc_serial_t ack_serial,
608                                      enum rxrpc_rtt_rx_trace type)
609 {
610         rxrpc_serial_t orig_serial;
611         unsigned long avail;
612         ktime_t sent_at;
613         bool matched = false;
614         int i;
615
616         avail = READ_ONCE(call->rtt_avail);
617         smp_rmb(); /* Read avail bits before accessing data. */
618
619         for (i = 0; i < ARRAY_SIZE(call->rtt_serial); i++) {
620                 if (!test_bit(i + RXRPC_CALL_RTT_PEND_SHIFT, &avail))
621                         continue;
622
623                 sent_at = call->rtt_sent_at[i];
624                 orig_serial = call->rtt_serial[i];
625
626                 if (orig_serial == acked_serial) {
627                         clear_bit(i + RXRPC_CALL_RTT_PEND_SHIFT, &call->rtt_avail);
628                         smp_mb(); /* Read data before setting avail bit */
629                         set_bit(i, &call->rtt_avail);
630                         if (type != rxrpc_rtt_rx_cancel)
631                                 rxrpc_peer_add_rtt(call, type, i, acked_serial, ack_serial,
632                                                    sent_at, resp_time);
633                         else
634                                 trace_rxrpc_rtt_rx(call, rxrpc_rtt_rx_cancel, i,
635                                                    orig_serial, acked_serial, 0, 0);
636                         matched = true;
637                 }
638
639                 /* If a later serial is being acked, then mark this slot as
640                  * being available.
641                  */
642                 if (after(acked_serial, orig_serial)) {
643                         trace_rxrpc_rtt_rx(call, rxrpc_rtt_rx_obsolete, i,
644                                            orig_serial, acked_serial, 0, 0);
645                         clear_bit(i + RXRPC_CALL_RTT_PEND_SHIFT, &call->rtt_avail);
646                         smp_wmb();
647                         set_bit(i, &call->rtt_avail);
648                 }
649         }
650
651         if (!matched)
652                 trace_rxrpc_rtt_rx(call, rxrpc_rtt_rx_lost, 9, 0, acked_serial, 0, 0);
653 }
654
655 /*
656  * Process the response to a ping that we sent to find out if we lost an ACK.
657  *
658  * If we got back a ping response that indicates a lower tx_top than what we
659  * had at the time of the ping transmission, we adjudge all the DATA packets
660  * sent between the response tx_top and the ping-time tx_top to have been lost.
661  */
662 static void rxrpc_input_check_for_lost_ack(struct rxrpc_call *call)
663 {
664         struct rxrpc_txbuf *txb;
665         rxrpc_seq_t top, bottom;
666         bool resend = false;
667
668         bottom = READ_ONCE(call->acks_hard_ack) + 1;
669         top = READ_ONCE(call->acks_lost_top);
670         if (before(bottom, top)) {
671                 list_for_each_entry_rcu(txb, &call->tx_buffer, call_link, false) {
672                         if (test_bit(RXRPC_TXBUF_ACKED, &txb->flags))
673                                 continue;
674                         set_bit(RXRPC_TXBUF_RETRANS, &txb->flags);
675                         resend = true;
676                 }
677         }
678
679         if (resend && !test_and_set_bit(RXRPC_CALL_EV_RESEND, &call->events))
680                 rxrpc_queue_call(call);
681 }
682
683 /*
684  * Process a ping response.
685  */
686 static void rxrpc_input_ping_response(struct rxrpc_call *call,
687                                       ktime_t resp_time,
688                                       rxrpc_serial_t acked_serial,
689                                       rxrpc_serial_t ack_serial)
690 {
691         if (acked_serial == call->acks_lost_ping)
692                 rxrpc_input_check_for_lost_ack(call);
693 }
694
695 /*
696  * Process the extra information that may be appended to an ACK packet
697  */
698 static void rxrpc_input_ackinfo(struct rxrpc_call *call, struct sk_buff *skb,
699                                 struct rxrpc_ackinfo *ackinfo)
700 {
701         struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
702         struct rxrpc_peer *peer;
703         unsigned int mtu;
704         bool wake = false;
705         u32 rwind = ntohl(ackinfo->rwind);
706
707         _proto("Rx ACK %%%u Info { rx=%u max=%u rwin=%u jm=%u }",
708                sp->hdr.serial,
709                ntohl(ackinfo->rxMTU), ntohl(ackinfo->maxMTU),
710                rwind, ntohl(ackinfo->jumbo_max));
711
712         if (rwind > RXRPC_TX_MAX_WINDOW)
713                 rwind = RXRPC_TX_MAX_WINDOW;
714         if (call->tx_winsize != rwind) {
715                 if (rwind > call->tx_winsize)
716                         wake = true;
717                 trace_rxrpc_rx_rwind_change(call, sp->hdr.serial, rwind, wake);
718                 call->tx_winsize = rwind;
719         }
720
721         if (call->cong_ssthresh > rwind)
722                 call->cong_ssthresh = rwind;
723
724         mtu = min(ntohl(ackinfo->rxMTU), ntohl(ackinfo->maxMTU));
725
726         peer = call->peer;
727         if (mtu < peer->maxdata) {
728                 spin_lock_bh(&peer->lock);
729                 peer->maxdata = mtu;
730                 peer->mtu = mtu + peer->hdrsize;
731                 spin_unlock_bh(&peer->lock);
732                 _net("Net MTU %u (maxdata %u)", peer->mtu, peer->maxdata);
733         }
734
735         if (wake)
736                 wake_up(&call->waitq);
737 }
738
739 /*
740  * Process individual soft ACKs.
741  *
742  * Each ACK in the array corresponds to one packet and can be either an ACK or
743  * a NAK.  If we get find an explicitly NAK'd packet we resend immediately;
744  * packets that lie beyond the end of the ACK list are scheduled for resend by
745  * the timer on the basis that the peer might just not have processed them at
746  * the time the ACK was sent.
747  */
748 static void rxrpc_input_soft_acks(struct rxrpc_call *call, u8 *acks,
749                                   rxrpc_seq_t seq, int nr_acks,
750                                   struct rxrpc_ack_summary *summary)
751 {
752         struct rxrpc_txbuf *txb;
753
754         list_for_each_entry_rcu(txb, &call->tx_buffer, call_link, false) {
755                 if (before(txb->seq, seq))
756                         continue;
757                 if (after_eq(txb->seq, seq + nr_acks))
758                         break;
759                 switch (acks[txb->seq - seq]) {
760                 case RXRPC_ACK_TYPE_ACK:
761                         summary->nr_acks++;
762                         if (test_bit(RXRPC_TXBUF_ACKED, &txb->flags))
763                                 continue;
764                         /* A lot of the time the packet is going to
765                          * have been ACK.'d already.
766                          */
767                         clear_bit(RXRPC_TXBUF_NACKED, &txb->flags);
768                         set_bit(RXRPC_TXBUF_ACKED, &txb->flags);
769                         summary->nr_new_acks++;
770                         break;
771                 case RXRPC_ACK_TYPE_NACK:
772                         if (!summary->nr_nacks &&
773                             call->acks_lowest_nak != seq) {
774                                 call->acks_lowest_nak = seq;
775                                 summary->new_low_nack = true;
776                         }
777                         summary->nr_nacks++;
778                         if (test_bit(RXRPC_TXBUF_NACKED, &txb->flags))
779                                 continue;
780                         summary->nr_new_nacks++;
781                         clear_bit(RXRPC_TXBUF_ACKED, &txb->flags);
782                         set_bit(RXRPC_TXBUF_NACKED, &txb->flags);
783                         set_bit(RXRPC_TXBUF_RETRANS, &txb->flags);
784                         break;
785                 default:
786                         return rxrpc_proto_abort("SFT", call, 0);
787                 }
788         }
789 }
790
791 /*
792  * Return true if the ACK is valid - ie. it doesn't appear to have regressed
793  * with respect to the ack state conveyed by preceding ACKs.
794  */
795 static bool rxrpc_is_ack_valid(struct rxrpc_call *call,
796                                rxrpc_seq_t first_pkt, rxrpc_seq_t prev_pkt)
797 {
798         rxrpc_seq_t base = READ_ONCE(call->acks_first_seq);
799
800         if (after(first_pkt, base))
801                 return true; /* The window advanced */
802
803         if (before(first_pkt, base))
804                 return false; /* firstPacket regressed */
805
806         if (after_eq(prev_pkt, call->acks_prev_seq))
807                 return true; /* previousPacket hasn't regressed. */
808
809         /* Some rx implementations put a serial number in previousPacket. */
810         if (after_eq(prev_pkt, base + call->tx_winsize))
811                 return false;
812         return true;
813 }
814
815 /*
816  * Process an ACK packet.
817  *
818  * ack.firstPacket is the sequence number of the first soft-ACK'd/NAK'd packet
819  * in the ACK array.  Anything before that is hard-ACK'd and may be discarded.
820  *
821  * A hard-ACK means that a packet has been processed and may be discarded; a
822  * soft-ACK means that the packet may be discarded and retransmission
823  * requested.  A phase is complete when all packets are hard-ACK'd.
824  */
825 static void rxrpc_input_ack(struct rxrpc_call *call, struct sk_buff *skb)
826 {
827         struct rxrpc_ack_summary summary = { 0 };
828         struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
829         union {
830                 struct rxrpc_ackpacket ack;
831                 struct rxrpc_ackinfo info;
832                 u8 acks[RXRPC_MAXACKS];
833         } buf;
834         rxrpc_serial_t ack_serial, acked_serial;
835         rxrpc_seq_t first_soft_ack, hard_ack, prev_pkt;
836         int nr_acks, offset, ioffset;
837
838         _enter("");
839
840         offset = sizeof(struct rxrpc_wire_header);
841         if (skb_copy_bits(skb, offset, &buf.ack, sizeof(buf.ack)) < 0) {
842                 _debug("extraction failure");
843                 return rxrpc_proto_abort("XAK", call, 0);
844         }
845         offset += sizeof(buf.ack);
846
847         ack_serial = sp->hdr.serial;
848         acked_serial = ntohl(buf.ack.serial);
849         first_soft_ack = ntohl(buf.ack.firstPacket);
850         prev_pkt = ntohl(buf.ack.previousPacket);
851         hard_ack = first_soft_ack - 1;
852         nr_acks = buf.ack.nAcks;
853         summary.ack_reason = (buf.ack.reason < RXRPC_ACK__INVALID ?
854                               buf.ack.reason : RXRPC_ACK__INVALID);
855
856         trace_rxrpc_rx_ack(call, ack_serial, acked_serial,
857                            first_soft_ack, prev_pkt,
858                            summary.ack_reason, nr_acks);
859         rxrpc_inc_stat(call->rxnet, stat_rx_acks[buf.ack.reason]);
860
861         switch (buf.ack.reason) {
862         case RXRPC_ACK_PING_RESPONSE:
863                 rxrpc_input_ping_response(call, skb->tstamp, acked_serial,
864                                           ack_serial);
865                 rxrpc_complete_rtt_probe(call, skb->tstamp, acked_serial, ack_serial,
866                                          rxrpc_rtt_rx_ping_response);
867                 break;
868         case RXRPC_ACK_REQUESTED:
869                 rxrpc_complete_rtt_probe(call, skb->tstamp, acked_serial, ack_serial,
870                                          rxrpc_rtt_rx_requested_ack);
871                 break;
872         default:
873                 if (acked_serial != 0)
874                         rxrpc_complete_rtt_probe(call, skb->tstamp, acked_serial, ack_serial,
875                                                  rxrpc_rtt_rx_cancel);
876                 break;
877         }
878
879         if (buf.ack.reason == RXRPC_ACK_PING) {
880                 _proto("Rx ACK %%%u PING Request", ack_serial);
881                 rxrpc_send_ACK(call, RXRPC_ACK_PING_RESPONSE, ack_serial,
882                                rxrpc_propose_ack_respond_to_ping);
883         } else if (sp->hdr.flags & RXRPC_REQUEST_ACK) {
884                 rxrpc_send_ACK(call, RXRPC_ACK_REQUESTED, ack_serial,
885                                rxrpc_propose_ack_respond_to_ack);
886         }
887
888         /* If we get an EXCEEDS_WINDOW ACK from the server, it probably
889          * indicates that the client address changed due to NAT.  The server
890          * lost the call because it switched to a different peer.
891          */
892         if (unlikely(buf.ack.reason == RXRPC_ACK_EXCEEDS_WINDOW) &&
893             first_soft_ack == 1 &&
894             prev_pkt == 0 &&
895             rxrpc_is_client_call(call)) {
896                 rxrpc_set_call_completion(call, RXRPC_CALL_REMOTELY_ABORTED,
897                                           0, -ENETRESET);
898                 return;
899         }
900
901         /* If we get an OUT_OF_SEQUENCE ACK from the server, that can also
902          * indicate a change of address.  However, we can retransmit the call
903          * if we still have it buffered to the beginning.
904          */
905         if (unlikely(buf.ack.reason == RXRPC_ACK_OUT_OF_SEQUENCE) &&
906             first_soft_ack == 1 &&
907             prev_pkt == 0 &&
908             call->acks_hard_ack == 0 &&
909             rxrpc_is_client_call(call)) {
910                 rxrpc_set_call_completion(call, RXRPC_CALL_REMOTELY_ABORTED,
911                                           0, -ENETRESET);
912                 return;
913         }
914
915         /* Discard any out-of-order or duplicate ACKs (outside lock). */
916         if (!rxrpc_is_ack_valid(call, first_soft_ack, prev_pkt)) {
917                 trace_rxrpc_rx_discard_ack(call->debug_id, ack_serial,
918                                            first_soft_ack, call->acks_first_seq,
919                                            prev_pkt, call->acks_prev_seq);
920                 return;
921         }
922
923         buf.info.rxMTU = 0;
924         ioffset = offset + nr_acks + 3;
925         if (skb->len >= ioffset + sizeof(buf.info) &&
926             skb_copy_bits(skb, ioffset, &buf.info, sizeof(buf.info)) < 0)
927                 return rxrpc_proto_abort("XAI", call, 0);
928
929         spin_lock(&call->input_lock);
930
931         /* Discard any out-of-order or duplicate ACKs (inside lock). */
932         if (!rxrpc_is_ack_valid(call, first_soft_ack, prev_pkt)) {
933                 trace_rxrpc_rx_discard_ack(call->debug_id, ack_serial,
934                                            first_soft_ack, call->acks_first_seq,
935                                            prev_pkt, call->acks_prev_seq);
936                 goto out;
937         }
938         call->acks_latest_ts = skb->tstamp;
939
940         call->acks_first_seq = first_soft_ack;
941         call->acks_prev_seq = prev_pkt;
942
943         if (buf.ack.reason != RXRPC_ACK_PING &&
944             after(acked_serial, call->acks_highest_serial))
945                 call->acks_highest_serial = acked_serial;
946
947         /* Parse rwind and mtu sizes if provided. */
948         if (buf.info.rxMTU)
949                 rxrpc_input_ackinfo(call, skb, &buf.info);
950
951         if (first_soft_ack == 0) {
952                 rxrpc_proto_abort("AK0", call, 0);
953                 goto out;
954         }
955
956         /* Ignore ACKs unless we are or have just been transmitting. */
957         switch (READ_ONCE(call->state)) {
958         case RXRPC_CALL_CLIENT_SEND_REQUEST:
959         case RXRPC_CALL_CLIENT_AWAIT_REPLY:
960         case RXRPC_CALL_SERVER_SEND_REPLY:
961         case RXRPC_CALL_SERVER_AWAIT_ACK:
962                 break;
963         default:
964                 goto out;
965         }
966
967         if (before(hard_ack, call->acks_hard_ack) ||
968             after(hard_ack, call->tx_top)) {
969                 rxrpc_proto_abort("AKW", call, 0);
970                 goto out;
971         }
972         if (nr_acks > call->tx_top - hard_ack) {
973                 rxrpc_proto_abort("AKN", call, 0);
974                 goto out;
975         }
976
977         if (after(hard_ack, call->acks_hard_ack)) {
978                 if (rxrpc_rotate_tx_window(call, hard_ack, &summary)) {
979                         rxrpc_end_tx_phase(call, false, "ETA");
980                         goto out;
981                 }
982         }
983
984         if (nr_acks > 0) {
985                 if (skb_copy_bits(skb, offset, buf.acks, nr_acks) < 0) {
986                         rxrpc_proto_abort("XSA", call, 0);
987                         goto out;
988                 }
989                 rxrpc_input_soft_acks(call, buf.acks, first_soft_ack, nr_acks,
990                                       &summary);
991         }
992
993         if (test_bit(RXRPC_CALL_TX_LAST, &call->flags) &&
994             summary.nr_acks == call->tx_top - hard_ack &&
995             rxrpc_is_client_call(call))
996                 rxrpc_propose_ping(call, ack_serial,
997                                    rxrpc_propose_ack_ping_for_lost_reply);
998
999         rxrpc_congestion_management(call, skb, &summary, acked_serial);
1000 out:
1001         spin_unlock(&call->input_lock);
1002 }
1003
1004 /*
1005  * Process an ACKALL packet.
1006  */
1007 static void rxrpc_input_ackall(struct rxrpc_call *call, struct sk_buff *skb)
1008 {
1009         struct rxrpc_ack_summary summary = { 0 };
1010         struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
1011
1012         _proto("Rx ACKALL %%%u", sp->hdr.serial);
1013
1014         spin_lock(&call->input_lock);
1015
1016         if (rxrpc_rotate_tx_window(call, call->tx_top, &summary))
1017                 rxrpc_end_tx_phase(call, false, "ETL");
1018
1019         spin_unlock(&call->input_lock);
1020 }
1021
1022 /*
1023  * Process an ABORT packet directed at a call.
1024  */
1025 static void rxrpc_input_abort(struct rxrpc_call *call, struct sk_buff *skb)
1026 {
1027         struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
1028         __be32 wtmp;
1029         u32 abort_code = RX_CALL_DEAD;
1030
1031         _enter("");
1032
1033         if (skb->len >= 4 &&
1034             skb_copy_bits(skb, sizeof(struct rxrpc_wire_header),
1035                           &wtmp, sizeof(wtmp)) >= 0)
1036                 abort_code = ntohl(wtmp);
1037
1038         trace_rxrpc_rx_abort(call, sp->hdr.serial, abort_code);
1039
1040         _proto("Rx ABORT %%%u { %x }", sp->hdr.serial, abort_code);
1041
1042         rxrpc_set_call_completion(call, RXRPC_CALL_REMOTELY_ABORTED,
1043                                   abort_code, -ECONNABORTED);
1044 }
1045
1046 /*
1047  * Process an incoming call packet.
1048  */
1049 static void rxrpc_input_call_packet(struct rxrpc_call *call,
1050                                     struct sk_buff *skb)
1051 {
1052         struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
1053         unsigned long timo;
1054
1055         _enter("%p,%p", call, skb);
1056
1057         timo = READ_ONCE(call->next_rx_timo);
1058         if (timo) {
1059                 unsigned long now = jiffies, expect_rx_by;
1060
1061                 expect_rx_by = now + timo;
1062                 WRITE_ONCE(call->expect_rx_by, expect_rx_by);
1063                 rxrpc_reduce_call_timer(call, expect_rx_by, now,
1064                                         rxrpc_timer_set_for_normal);
1065         }
1066
1067         switch (sp->hdr.type) {
1068         case RXRPC_PACKET_TYPE_DATA:
1069                 rxrpc_input_data(call, skb);
1070                 goto no_free;
1071
1072         case RXRPC_PACKET_TYPE_ACK:
1073                 rxrpc_input_ack(call, skb);
1074                 break;
1075
1076         case RXRPC_PACKET_TYPE_BUSY:
1077                 _proto("Rx BUSY %%%u", sp->hdr.serial);
1078
1079                 /* Just ignore BUSY packets from the server; the retry and
1080                  * lifespan timers will take care of business.  BUSY packets
1081                  * from the client don't make sense.
1082                  */
1083                 break;
1084
1085         case RXRPC_PACKET_TYPE_ABORT:
1086                 rxrpc_input_abort(call, skb);
1087                 break;
1088
1089         case RXRPC_PACKET_TYPE_ACKALL:
1090                 rxrpc_input_ackall(call, skb);
1091                 break;
1092
1093         default:
1094                 break;
1095         }
1096
1097         rxrpc_free_skb(skb, rxrpc_skb_freed);
1098 no_free:
1099         _leave("");
1100 }
1101
1102 /*
1103  * Handle a new service call on a channel implicitly completing the preceding
1104  * call on that channel.  This does not apply to client conns.
1105  *
1106  * TODO: If callNumber > call_id + 1, renegotiate security.
1107  */
1108 static void rxrpc_input_implicit_end_call(struct rxrpc_sock *rx,
1109                                           struct rxrpc_connection *conn,
1110                                           struct rxrpc_call *call)
1111 {
1112         switch (READ_ONCE(call->state)) {
1113         case RXRPC_CALL_SERVER_AWAIT_ACK:
1114                 rxrpc_call_completed(call);
1115                 fallthrough;
1116         case RXRPC_CALL_COMPLETE:
1117                 break;
1118         default:
1119                 if (rxrpc_abort_call("IMP", call, 0, RX_CALL_DEAD, -ESHUTDOWN)) {
1120                         set_bit(RXRPC_CALL_EV_ABORT, &call->events);
1121                         rxrpc_queue_call(call);
1122                 }
1123                 trace_rxrpc_improper_term(call);
1124                 break;
1125         }
1126
1127         spin_lock(&rx->incoming_lock);
1128         __rxrpc_disconnect_call(conn, call);
1129         spin_unlock(&rx->incoming_lock);
1130 }
1131
1132 /*
1133  * post connection-level events to the connection
1134  * - this includes challenges, responses, some aborts and call terminal packet
1135  *   retransmission.
1136  */
1137 static void rxrpc_post_packet_to_conn(struct rxrpc_connection *conn,
1138                                       struct sk_buff *skb)
1139 {
1140         _enter("%p,%p", conn, skb);
1141
1142         skb_queue_tail(&conn->rx_queue, skb);
1143         rxrpc_queue_conn(conn);
1144 }
1145
1146 /*
1147  * post endpoint-level events to the local endpoint
1148  * - this includes debug and version messages
1149  */
1150 static void rxrpc_post_packet_to_local(struct rxrpc_local *local,
1151                                        struct sk_buff *skb)
1152 {
1153         _enter("%p,%p", local, skb);
1154
1155         if (rxrpc_get_local_maybe(local)) {
1156                 skb_queue_tail(&local->event_queue, skb);
1157                 rxrpc_queue_local(local);
1158         } else {
1159                 rxrpc_free_skb(skb, rxrpc_skb_freed);
1160         }
1161 }
1162
1163 /*
1164  * put a packet up for transport-level abort
1165  */
1166 static void rxrpc_reject_packet(struct rxrpc_local *local, struct sk_buff *skb)
1167 {
1168         if (rxrpc_get_local_maybe(local)) {
1169                 skb_queue_tail(&local->reject_queue, skb);
1170                 rxrpc_queue_local(local);
1171         } else {
1172                 rxrpc_free_skb(skb, rxrpc_skb_freed);
1173         }
1174 }
1175
1176 /*
1177  * Extract the wire header from a packet and translate the byte order.
1178  */
1179 static noinline
1180 int rxrpc_extract_header(struct rxrpc_skb_priv *sp, struct sk_buff *skb)
1181 {
1182         struct rxrpc_wire_header whdr;
1183
1184         /* dig out the RxRPC connection details */
1185         if (skb_copy_bits(skb, 0, &whdr, sizeof(whdr)) < 0) {
1186                 trace_rxrpc_rx_eproto(NULL, sp->hdr.serial,
1187                                       tracepoint_string("bad_hdr"));
1188                 return -EBADMSG;
1189         }
1190
1191         memset(sp, 0, sizeof(*sp));
1192         sp->hdr.epoch           = ntohl(whdr.epoch);
1193         sp->hdr.cid             = ntohl(whdr.cid);
1194         sp->hdr.callNumber      = ntohl(whdr.callNumber);
1195         sp->hdr.seq             = ntohl(whdr.seq);
1196         sp->hdr.serial          = ntohl(whdr.serial);
1197         sp->hdr.flags           = whdr.flags;
1198         sp->hdr.type            = whdr.type;
1199         sp->hdr.userStatus      = whdr.userStatus;
1200         sp->hdr.securityIndex   = whdr.securityIndex;
1201         sp->hdr._rsvd           = ntohs(whdr._rsvd);
1202         sp->hdr.serviceId       = ntohs(whdr.serviceId);
1203         return 0;
1204 }
1205
1206 /*
1207  * handle data received on the local endpoint
1208  * - may be called in interrupt context
1209  *
1210  * [!] Note that as this is called from the encap_rcv hook, the socket is not
1211  * held locked by the caller and nothing prevents sk_user_data on the UDP from
1212  * being cleared in the middle of processing this function.
1213  *
1214  * Called with the RCU read lock held from the IP layer via UDP.
1215  */
1216 int rxrpc_input_packet(struct sock *udp_sk, struct sk_buff *skb)
1217 {
1218         struct rxrpc_local *local = rcu_dereference_sk_user_data(udp_sk);
1219         struct rxrpc_connection *conn;
1220         struct rxrpc_channel *chan;
1221         struct rxrpc_call *call = NULL;
1222         struct rxrpc_skb_priv *sp;
1223         struct rxrpc_peer *peer = NULL;
1224         struct rxrpc_sock *rx = NULL;
1225         unsigned int channel;
1226
1227         _enter("%p", udp_sk);
1228
1229         if (unlikely(!local)) {
1230                 kfree_skb(skb);
1231                 return 0;
1232         }
1233         if (skb->tstamp == 0)
1234                 skb->tstamp = ktime_get_real();
1235
1236         rxrpc_new_skb(skb, rxrpc_skb_received);
1237
1238         skb_pull(skb, sizeof(struct udphdr));
1239
1240         /* The UDP protocol already released all skb resources;
1241          * we are free to add our own data there.
1242          */
1243         sp = rxrpc_skb(skb);
1244
1245         /* dig out the RxRPC connection details */
1246         if (rxrpc_extract_header(sp, skb) < 0)
1247                 goto bad_message;
1248
1249         if (IS_ENABLED(CONFIG_AF_RXRPC_INJECT_LOSS)) {
1250                 static int lose;
1251                 if ((lose++ & 7) == 7) {
1252                         trace_rxrpc_rx_lose(sp);
1253                         rxrpc_free_skb(skb, rxrpc_skb_lost);
1254                         return 0;
1255                 }
1256         }
1257
1258         if (skb->tstamp == 0)
1259                 skb->tstamp = ktime_get_real();
1260         trace_rxrpc_rx_packet(sp);
1261
1262         switch (sp->hdr.type) {
1263         case RXRPC_PACKET_TYPE_VERSION:
1264                 if (rxrpc_to_client(sp))
1265                         goto discard;
1266                 rxrpc_post_packet_to_local(local, skb);
1267                 goto out;
1268
1269         case RXRPC_PACKET_TYPE_BUSY:
1270                 if (rxrpc_to_server(sp))
1271                         goto discard;
1272                 fallthrough;
1273         case RXRPC_PACKET_TYPE_ACK:
1274         case RXRPC_PACKET_TYPE_ACKALL:
1275                 if (sp->hdr.callNumber == 0)
1276                         goto bad_message;
1277                 fallthrough;
1278         case RXRPC_PACKET_TYPE_ABORT:
1279                 break;
1280
1281         case RXRPC_PACKET_TYPE_DATA:
1282                 if (sp->hdr.callNumber == 0 ||
1283                     sp->hdr.seq == 0)
1284                         goto bad_message;
1285
1286                 /* Unshare the packet so that it can be modified for in-place
1287                  * decryption.
1288                  */
1289                 if (sp->hdr.securityIndex != 0) {
1290                         struct sk_buff *nskb = skb_unshare(skb, GFP_ATOMIC);
1291                         if (!nskb) {
1292                                 rxrpc_eaten_skb(skb, rxrpc_skb_unshared_nomem);
1293                                 goto out;
1294                         }
1295
1296                         if (nskb != skb) {
1297                                 rxrpc_eaten_skb(skb, rxrpc_skb_received);
1298                                 skb = nskb;
1299                                 rxrpc_new_skb(skb, rxrpc_skb_unshared);
1300                                 sp = rxrpc_skb(skb);
1301                         }
1302                 }
1303                 break;
1304
1305         case RXRPC_PACKET_TYPE_CHALLENGE:
1306                 if (rxrpc_to_server(sp))
1307                         goto discard;
1308                 break;
1309         case RXRPC_PACKET_TYPE_RESPONSE:
1310                 if (rxrpc_to_client(sp))
1311                         goto discard;
1312                 break;
1313
1314                 /* Packet types 9-11 should just be ignored. */
1315         case RXRPC_PACKET_TYPE_PARAMS:
1316         case RXRPC_PACKET_TYPE_10:
1317         case RXRPC_PACKET_TYPE_11:
1318                 goto discard;
1319
1320         default:
1321                 _proto("Rx Bad Packet Type %u", sp->hdr.type);
1322                 goto bad_message;
1323         }
1324
1325         if (sp->hdr.serviceId == 0)
1326                 goto bad_message;
1327
1328         if (rxrpc_to_server(sp)) {
1329                 /* Weed out packets to services we're not offering.  Packets
1330                  * that would begin a call are explicitly rejected and the rest
1331                  * are just discarded.
1332                  */
1333                 rx = rcu_dereference(local->service);
1334                 if (!rx || (sp->hdr.serviceId != rx->srx.srx_service &&
1335                             sp->hdr.serviceId != rx->second_service)) {
1336                         if (sp->hdr.type == RXRPC_PACKET_TYPE_DATA &&
1337                             sp->hdr.seq == 1)
1338                                 goto unsupported_service;
1339                         goto discard;
1340                 }
1341         }
1342
1343         conn = rxrpc_find_connection_rcu(local, skb, &peer);
1344         if (conn) {
1345                 if (sp->hdr.securityIndex != conn->security_ix)
1346                         goto wrong_security;
1347
1348                 if (sp->hdr.serviceId != conn->service_id) {
1349                         int old_id;
1350
1351                         if (!test_bit(RXRPC_CONN_PROBING_FOR_UPGRADE, &conn->flags))
1352                                 goto reupgrade;
1353                         old_id = cmpxchg(&conn->service_id, conn->params.service_id,
1354                                          sp->hdr.serviceId);
1355
1356                         if (old_id != conn->params.service_id &&
1357                             old_id != sp->hdr.serviceId)
1358                                 goto reupgrade;
1359                 }
1360
1361                 if (sp->hdr.callNumber == 0) {
1362                         /* Connection-level packet */
1363                         _debug("CONN %p {%d}", conn, conn->debug_id);
1364                         rxrpc_post_packet_to_conn(conn, skb);
1365                         goto out;
1366                 }
1367
1368                 if ((int)sp->hdr.serial - (int)conn->hi_serial > 0)
1369                         conn->hi_serial = sp->hdr.serial;
1370
1371                 /* Call-bound packets are routed by connection channel. */
1372                 channel = sp->hdr.cid & RXRPC_CHANNELMASK;
1373                 chan = &conn->channels[channel];
1374
1375                 /* Ignore really old calls */
1376                 if (sp->hdr.callNumber < chan->last_call)
1377                         goto discard;
1378
1379                 if (sp->hdr.callNumber == chan->last_call) {
1380                         if (chan->call ||
1381                             sp->hdr.type == RXRPC_PACKET_TYPE_ABORT)
1382                                 goto discard;
1383
1384                         /* For the previous service call, if completed
1385                          * successfully, we discard all further packets.
1386                          */
1387                         if (rxrpc_conn_is_service(conn) &&
1388                             chan->last_type == RXRPC_PACKET_TYPE_ACK)
1389                                 goto discard;
1390
1391                         /* But otherwise we need to retransmit the final packet
1392                          * from data cached in the connection record.
1393                          */
1394                         if (sp->hdr.type == RXRPC_PACKET_TYPE_DATA)
1395                                 trace_rxrpc_rx_data(chan->call_debug_id,
1396                                                     sp->hdr.seq,
1397                                                     sp->hdr.serial,
1398                                                     sp->hdr.flags);
1399                         rxrpc_post_packet_to_conn(conn, skb);
1400                         goto out;
1401                 }
1402
1403                 call = rcu_dereference(chan->call);
1404
1405                 if (sp->hdr.callNumber > chan->call_id) {
1406                         if (rxrpc_to_client(sp))
1407                                 goto reject_packet;
1408                         if (call)
1409                                 rxrpc_input_implicit_end_call(rx, conn, call);
1410                         call = NULL;
1411                 }
1412
1413                 if (call) {
1414                         if (sp->hdr.serviceId != call->service_id)
1415                                 call->service_id = sp->hdr.serviceId;
1416                         if ((int)sp->hdr.serial - (int)call->rx_serial > 0)
1417                                 call->rx_serial = sp->hdr.serial;
1418                         if (!test_bit(RXRPC_CALL_RX_HEARD, &call->flags))
1419                                 set_bit(RXRPC_CALL_RX_HEARD, &call->flags);
1420                 }
1421         }
1422
1423         if (!call || refcount_read(&call->ref) == 0) {
1424                 if (rxrpc_to_client(sp) ||
1425                     sp->hdr.type != RXRPC_PACKET_TYPE_DATA)
1426                         goto bad_message;
1427                 if (sp->hdr.seq != 1)
1428                         goto discard;
1429                 call = rxrpc_new_incoming_call(local, rx, skb);
1430                 if (!call)
1431                         goto reject_packet;
1432         }
1433
1434         /* Process a call packet; this either discards or passes on the ref
1435          * elsewhere.
1436          */
1437         rxrpc_input_call_packet(call, skb);
1438         goto out;
1439
1440 discard:
1441         rxrpc_free_skb(skb, rxrpc_skb_freed);
1442 out:
1443         trace_rxrpc_rx_done(0, 0);
1444         return 0;
1445
1446 wrong_security:
1447         trace_rxrpc_abort(0, "SEC", sp->hdr.cid, sp->hdr.callNumber, sp->hdr.seq,
1448                           RXKADINCONSISTENCY, EBADMSG);
1449         skb->priority = RXKADINCONSISTENCY;
1450         goto post_abort;
1451
1452 unsupported_service:
1453         trace_rxrpc_abort(0, "INV", sp->hdr.cid, sp->hdr.callNumber, sp->hdr.seq,
1454                           RX_INVALID_OPERATION, EOPNOTSUPP);
1455         skb->priority = RX_INVALID_OPERATION;
1456         goto post_abort;
1457
1458 reupgrade:
1459         trace_rxrpc_abort(0, "UPG", sp->hdr.cid, sp->hdr.callNumber, sp->hdr.seq,
1460                           RX_PROTOCOL_ERROR, EBADMSG);
1461         goto protocol_error;
1462
1463 bad_message:
1464         trace_rxrpc_abort(0, "BAD", sp->hdr.cid, sp->hdr.callNumber, sp->hdr.seq,
1465                           RX_PROTOCOL_ERROR, EBADMSG);
1466 protocol_error:
1467         skb->priority = RX_PROTOCOL_ERROR;
1468 post_abort:
1469         skb->mark = RXRPC_SKB_MARK_REJECT_ABORT;
1470 reject_packet:
1471         trace_rxrpc_rx_done(skb->mark, skb->priority);
1472         rxrpc_reject_packet(local, skb);
1473         _leave(" [badmsg]");
1474         return 0;
1475 }