c1f83d22f9b795beee015614fc00b7c8d5e7016d
[cascardo/linux.git] / net / rxrpc / input.c
1 /* RxRPC packet reception
2  *
3  * Copyright (C) 2007, 2016 Red Hat, Inc. All Rights Reserved.
4  * Written by David Howells (dhowells@redhat.com)
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version
9  * 2 of the License, or (at your option) any later version.
10  */
11
12 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13
14 #include <linux/module.h>
15 #include <linux/net.h>
16 #include <linux/skbuff.h>
17 #include <linux/errqueue.h>
18 #include <linux/udp.h>
19 #include <linux/in.h>
20 #include <linux/in6.h>
21 #include <linux/icmp.h>
22 #include <linux/gfp.h>
23 #include <net/sock.h>
24 #include <net/af_rxrpc.h>
25 #include <net/ip.h>
26 #include <net/udp.h>
27 #include <net/net_namespace.h>
28 #include "ar-internal.h"
29
30 static void rxrpc_proto_abort(const char *why,
31                               struct rxrpc_call *call, rxrpc_seq_t seq)
32 {
33         if (rxrpc_abort_call(why, call, seq, RX_PROTOCOL_ERROR, EBADMSG)) {
34                 set_bit(RXRPC_CALL_EV_ABORT, &call->events);
35                 rxrpc_queue_call(call);
36         }
37 }
38
39 /*
40  * Apply a hard ACK by advancing the Tx window.
41  */
42 static void rxrpc_rotate_tx_window(struct rxrpc_call *call, rxrpc_seq_t to)
43 {
44         struct sk_buff *skb, *list = NULL;
45         int ix;
46
47         spin_lock(&call->lock);
48
49         while (before(call->tx_hard_ack, to)) {
50                 call->tx_hard_ack++;
51                 ix = call->tx_hard_ack & RXRPC_RXTX_BUFF_MASK;
52                 skb = call->rxtx_buffer[ix];
53                 rxrpc_see_skb(skb);
54                 call->rxtx_buffer[ix] = NULL;
55                 call->rxtx_annotations[ix] = 0;
56                 skb->next = list;
57                 list = skb;
58         }
59
60         spin_unlock(&call->lock);
61
62         wake_up(&call->waitq);
63
64         while (list) {
65                 skb = list;
66                 list = skb->next;
67                 skb->next = NULL;
68                 rxrpc_free_skb(skb);
69         }
70 }
71
72 /*
73  * End the transmission phase of a call.
74  *
75  * This occurs when we get an ACKALL packet, the first DATA packet of a reply,
76  * or a final ACK packet.
77  */
78 static bool rxrpc_end_tx_phase(struct rxrpc_call *call, const char *abort_why)
79 {
80         _enter("");
81
82         switch (call->state) {
83         case RXRPC_CALL_CLIENT_RECV_REPLY:
84                 return true;
85         case RXRPC_CALL_CLIENT_AWAIT_REPLY:
86         case RXRPC_CALL_SERVER_AWAIT_ACK:
87                 break;
88         default:
89                 rxrpc_proto_abort(abort_why, call, call->tx_top);
90                 return false;
91         }
92
93         rxrpc_rotate_tx_window(call, call->tx_top);
94
95         write_lock(&call->state_lock);
96
97         switch (call->state) {
98         default:
99                 break;
100         case RXRPC_CALL_CLIENT_AWAIT_REPLY:
101                 call->state = RXRPC_CALL_CLIENT_RECV_REPLY;
102                 break;
103         case RXRPC_CALL_SERVER_AWAIT_ACK:
104                 __rxrpc_call_completed(call);
105                 rxrpc_notify_socket(call);
106                 break;
107         }
108
109         write_unlock(&call->state_lock);
110         _leave(" = ok");
111         return true;
112 }
113
114 /*
115  * Scan a jumbo packet to validate its structure and to work out how many
116  * subpackets it contains.
117  *
118  * A jumbo packet is a collection of consecutive packets glued together with
119  * little headers between that indicate how to change the initial header for
120  * each subpacket.
121  *
122  * RXRPC_JUMBO_PACKET must be set on all but the last subpacket - and all but
123  * the last are RXRPC_JUMBO_DATALEN in size.  The last subpacket may be of any
124  * size.
125  */
126 static bool rxrpc_validate_jumbo(struct sk_buff *skb)
127 {
128         struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
129         unsigned int offset = sp->offset;
130         unsigned int len = skb->len;
131         int nr_jumbo = 1;
132         u8 flags = sp->hdr.flags;
133
134         do {
135                 nr_jumbo++;
136                 if (len - offset < RXRPC_JUMBO_SUBPKTLEN)
137                         goto protocol_error;
138                 if (flags & RXRPC_LAST_PACKET)
139                         goto protocol_error;
140                 offset += RXRPC_JUMBO_DATALEN;
141                 if (skb_copy_bits(skb, offset, &flags, 1) < 0)
142                         goto protocol_error;
143                 offset += sizeof(struct rxrpc_jumbo_header);
144         } while (flags & RXRPC_JUMBO_PACKET);
145
146         sp->nr_jumbo = nr_jumbo;
147         return true;
148
149 protocol_error:
150         return false;
151 }
152
153 /*
154  * Handle reception of a duplicate packet.
155  *
156  * We have to take care to avoid an attack here whereby we're given a series of
157  * jumbograms, each with a sequence number one before the preceding one and
158  * filled up to maximum UDP size.  If they never send us the first packet in
159  * the sequence, they can cause us to have to hold on to around 2MiB of kernel
160  * space until the call times out.
161  *
162  * We limit the space usage by only accepting three duplicate jumbo packets per
163  * call.  After that, we tell the other side we're no longer accepting jumbos
164  * (that information is encoded in the ACK packet).
165  */
166 static void rxrpc_input_dup_data(struct rxrpc_call *call, rxrpc_seq_t seq,
167                                  u8 annotation, bool *_jumbo_bad)
168 {
169         /* Discard normal packets that are duplicates. */
170         if (annotation == 0)
171                 return;
172
173         /* Skip jumbo subpackets that are duplicates.  When we've had three or
174          * more partially duplicate jumbo packets, we refuse to take any more
175          * jumbos for this call.
176          */
177         if (!*_jumbo_bad) {
178                 call->nr_jumbo_bad++;
179                 *_jumbo_bad = true;
180         }
181 }
182
183 /*
184  * Process a DATA packet, adding the packet to the Rx ring.
185  */
186 static void rxrpc_input_data(struct rxrpc_call *call, struct sk_buff *skb,
187                              u16 skew)
188 {
189         struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
190         unsigned int offset = sp->offset;
191         unsigned int ix;
192         rxrpc_serial_t serial = sp->hdr.serial, ack_serial = 0;
193         rxrpc_seq_t seq = sp->hdr.seq, hard_ack;
194         bool immediate_ack = false, jumbo_bad = false, queued;
195         u16 len;
196         u8 ack = 0, flags, annotation = 0;
197
198         _enter("{%u,%u},{%u,%u}",
199                call->rx_hard_ack, call->rx_top, skb->len, seq);
200
201         _proto("Rx DATA %%%u { #%u f=%02x }",
202                sp->hdr.serial, seq, sp->hdr.flags);
203
204         if (call->state >= RXRPC_CALL_COMPLETE)
205                 return;
206
207         /* Received data implicitly ACKs all of the request packets we sent
208          * when we're acting as a client.
209          */
210         if (call->state == RXRPC_CALL_CLIENT_AWAIT_REPLY &&
211             !rxrpc_end_tx_phase(call, "ETD"))
212                 return;
213
214         call->ackr_prev_seq = seq;
215
216         hard_ack = READ_ONCE(call->rx_hard_ack);
217         if (after(seq, hard_ack + call->rx_winsize)) {
218                 ack = RXRPC_ACK_EXCEEDS_WINDOW;
219                 ack_serial = serial;
220                 goto ack;
221         }
222
223         flags = sp->hdr.flags;
224         if (flags & RXRPC_JUMBO_PACKET) {
225                 if (call->nr_jumbo_bad > 3) {
226                         ack = RXRPC_ACK_NOSPACE;
227                         ack_serial = serial;
228                         goto ack;
229                 }
230                 annotation = 1;
231         }
232
233 next_subpacket:
234         queued = false;
235         ix = seq & RXRPC_RXTX_BUFF_MASK;
236         len = skb->len;
237         if (flags & RXRPC_JUMBO_PACKET)
238                 len = RXRPC_JUMBO_DATALEN;
239
240         if (flags & RXRPC_LAST_PACKET) {
241                 if (test_bit(RXRPC_CALL_RX_LAST, &call->flags) &&
242                     seq != call->rx_top)
243                         return rxrpc_proto_abort("LSN", call, seq);
244         } else {
245                 if (test_bit(RXRPC_CALL_RX_LAST, &call->flags) &&
246                     after_eq(seq, call->rx_top))
247                         return rxrpc_proto_abort("LSA", call, seq);
248         }
249
250         if (before_eq(seq, hard_ack)) {
251                 ack = RXRPC_ACK_DUPLICATE;
252                 ack_serial = serial;
253                 goto skip;
254         }
255
256         if (flags & RXRPC_REQUEST_ACK && !ack) {
257                 ack = RXRPC_ACK_REQUESTED;
258                 ack_serial = serial;
259         }
260
261         if (call->rxtx_buffer[ix]) {
262                 rxrpc_input_dup_data(call, seq, annotation, &jumbo_bad);
263                 if (ack != RXRPC_ACK_DUPLICATE) {
264                         ack = RXRPC_ACK_DUPLICATE;
265                         ack_serial = serial;
266                 }
267                 immediate_ack = true;
268                 goto skip;
269         }
270
271         /* Queue the packet.  We use a couple of memory barriers here as need
272          * to make sure that rx_top is perceived to be set after the buffer
273          * pointer and that the buffer pointer is set after the annotation and
274          * the skb data.
275          *
276          * Barriers against rxrpc_recvmsg_data() and rxrpc_rotate_rx_window()
277          * and also rxrpc_fill_out_ack().
278          */
279         rxrpc_get_skb(skb);
280         call->rxtx_annotations[ix] = annotation;
281         smp_wmb();
282         call->rxtx_buffer[ix] = skb;
283         if (after(seq, call->rx_top))
284                 smp_store_release(&call->rx_top, seq);
285         if (flags & RXRPC_LAST_PACKET)
286                 set_bit(RXRPC_CALL_RX_LAST, &call->flags);
287         queued = true;
288
289         if (after_eq(seq, call->rx_expect_next)) {
290                 if (after(seq, call->rx_expect_next)) {
291                         _net("OOS %u > %u", seq, call->rx_expect_next);
292                         ack = RXRPC_ACK_OUT_OF_SEQUENCE;
293                         ack_serial = serial;
294                 }
295                 call->rx_expect_next = seq + 1;
296         }
297
298 skip:
299         offset += len;
300         if (flags & RXRPC_JUMBO_PACKET) {
301                 if (skb_copy_bits(skb, offset, &flags, 1) < 0)
302                         return rxrpc_proto_abort("XJF", call, seq);
303                 offset += sizeof(struct rxrpc_jumbo_header);
304                 seq++;
305                 serial++;
306                 annotation++;
307                 if (flags & RXRPC_JUMBO_PACKET)
308                         annotation |= RXRPC_RX_ANNO_JLAST;
309                 if (after(seq, hard_ack + call->rx_winsize)) {
310                         ack = RXRPC_ACK_EXCEEDS_WINDOW;
311                         ack_serial = serial;
312                         if (!jumbo_bad) {
313                                 call->nr_jumbo_bad++;
314                                 jumbo_bad = true;
315                         }
316                         goto ack;
317                 }
318
319                 _proto("Rx DATA Jumbo %%%u", serial);
320                 goto next_subpacket;
321         }
322
323         if (queued && flags & RXRPC_LAST_PACKET && !ack) {
324                 ack = RXRPC_ACK_DELAY;
325                 ack_serial = serial;
326         }
327
328 ack:
329         if (ack)
330                 rxrpc_propose_ACK(call, ack, skew, ack_serial,
331                                   immediate_ack, true);
332
333         if (sp->hdr.seq == READ_ONCE(call->rx_hard_ack) + 1)
334                 rxrpc_notify_socket(call);
335         _leave(" [queued]");
336 }
337
338 /*
339  * Process the extra information that may be appended to an ACK packet
340  */
341 static void rxrpc_input_ackinfo(struct rxrpc_call *call, struct sk_buff *skb,
342                                 struct rxrpc_ackinfo *ackinfo)
343 {
344         struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
345         struct rxrpc_peer *peer;
346         unsigned int mtu;
347         u32 rwind = ntohl(ackinfo->rwind);
348
349         _proto("Rx ACK %%%u Info { rx=%u max=%u rwin=%u jm=%u }",
350                sp->hdr.serial,
351                ntohl(ackinfo->rxMTU), ntohl(ackinfo->maxMTU),
352                rwind, ntohl(ackinfo->jumbo_max));
353
354         if (rwind > RXRPC_RXTX_BUFF_SIZE - 1)
355                 rwind = RXRPC_RXTX_BUFF_SIZE - 1;
356         call->tx_winsize = rwind;
357
358         mtu = min(ntohl(ackinfo->rxMTU), ntohl(ackinfo->maxMTU));
359
360         peer = call->peer;
361         if (mtu < peer->maxdata) {
362                 spin_lock_bh(&peer->lock);
363                 peer->maxdata = mtu;
364                 peer->mtu = mtu + peer->hdrsize;
365                 spin_unlock_bh(&peer->lock);
366                 _net("Net MTU %u (maxdata %u)", peer->mtu, peer->maxdata);
367         }
368 }
369
370 /*
371  * Process individual soft ACKs.
372  *
373  * Each ACK in the array corresponds to one packet and can be either an ACK or
374  * a NAK.  If we get find an explicitly NAK'd packet we resend immediately;
375  * packets that lie beyond the end of the ACK list are scheduled for resend by
376  * the timer on the basis that the peer might just not have processed them at
377  * the time the ACK was sent.
378  */
379 static void rxrpc_input_soft_acks(struct rxrpc_call *call, u8 *acks,
380                                   rxrpc_seq_t seq, int nr_acks)
381 {
382         bool resend = false;
383         int ix;
384
385         for (; nr_acks > 0; nr_acks--, seq++) {
386                 ix = seq & RXRPC_RXTX_BUFF_MASK;
387                 switch (*acks++) {
388                 case RXRPC_ACK_TYPE_ACK:
389                         call->rxtx_annotations[ix] = RXRPC_TX_ANNO_ACK;
390                         break;
391                 case RXRPC_ACK_TYPE_NACK:
392                         if (call->rxtx_annotations[ix] == RXRPC_TX_ANNO_NAK)
393                                 continue;
394                         call->rxtx_annotations[ix] = RXRPC_TX_ANNO_NAK;
395                         resend = true;
396                         break;
397                 default:
398                         return rxrpc_proto_abort("SFT", call, 0);
399                 }
400         }
401
402         if (resend &&
403             !test_and_set_bit(RXRPC_CALL_EV_RESEND, &call->events))
404                 rxrpc_queue_call(call);
405 }
406
407 /*
408  * Process an ACK packet.
409  *
410  * ack.firstPacket is the sequence number of the first soft-ACK'd/NAK'd packet
411  * in the ACK array.  Anything before that is hard-ACK'd and may be discarded.
412  *
413  * A hard-ACK means that a packet has been processed and may be discarded; a
414  * soft-ACK means that the packet may be discarded and retransmission
415  * requested.  A phase is complete when all packets are hard-ACK'd.
416  */
417 static void rxrpc_input_ack(struct rxrpc_call *call, struct sk_buff *skb,
418                             u16 skew)
419 {
420         struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
421         union {
422                 struct rxrpc_ackpacket ack;
423                 struct rxrpc_ackinfo info;
424                 u8 acks[RXRPC_MAXACKS];
425         } buf;
426         rxrpc_seq_t first_soft_ack, hard_ack;
427         int nr_acks, offset;
428
429         _enter("");
430
431         if (skb_copy_bits(skb, sp->offset, &buf.ack, sizeof(buf.ack)) < 0) {
432                 _debug("extraction failure");
433                 return rxrpc_proto_abort("XAK", call, 0);
434         }
435         sp->offset += sizeof(buf.ack);
436
437         first_soft_ack = ntohl(buf.ack.firstPacket);
438         hard_ack = first_soft_ack - 1;
439         nr_acks = buf.ack.nAcks;
440
441         _proto("Rx ACK %%%u { m=%hu f=#%u p=#%u s=%%%u r=%s n=%u }",
442                sp->hdr.serial,
443                ntohs(buf.ack.maxSkew),
444                first_soft_ack,
445                ntohl(buf.ack.previousPacket),
446                ntohl(buf.ack.serial),
447                rxrpc_acks(buf.ack.reason),
448                buf.ack.nAcks);
449
450         if (buf.ack.reason == RXRPC_ACK_PING) {
451                 _proto("Rx ACK %%%u PING Request", sp->hdr.serial);
452                 rxrpc_propose_ACK(call, RXRPC_ACK_PING_RESPONSE,
453                                   skew, sp->hdr.serial, true, true);
454         } else if (sp->hdr.flags & RXRPC_REQUEST_ACK) {
455                 rxrpc_propose_ACK(call, RXRPC_ACK_REQUESTED,
456                                   skew, sp->hdr.serial, true, true);
457         }
458
459         offset = sp->offset + nr_acks + 3;
460         if (skb->len >= offset + sizeof(buf.info)) {
461                 if (skb_copy_bits(skb, offset, &buf.info, sizeof(buf.info)) < 0)
462                         return rxrpc_proto_abort("XAI", call, 0);
463                 rxrpc_input_ackinfo(call, skb, &buf.info);
464         }
465
466         if (first_soft_ack == 0)
467                 return rxrpc_proto_abort("AK0", call, 0);
468
469         /* Ignore ACKs unless we are or have just been transmitting. */
470         switch (call->state) {
471         case RXRPC_CALL_CLIENT_SEND_REQUEST:
472         case RXRPC_CALL_CLIENT_AWAIT_REPLY:
473         case RXRPC_CALL_SERVER_SEND_REPLY:
474         case RXRPC_CALL_SERVER_AWAIT_ACK:
475                 break;
476         default:
477                 return;
478         }
479
480         /* Discard any out-of-order or duplicate ACKs. */
481         if ((int)sp->hdr.serial - (int)call->acks_latest <= 0) {
482                 _debug("discard ACK %d <= %d",
483                        sp->hdr.serial, call->acks_latest);
484                 return;
485         }
486         call->acks_latest = sp->hdr.serial;
487
488         if (test_bit(RXRPC_CALL_TX_LAST, &call->flags) &&
489             hard_ack == call->tx_top) {
490                 rxrpc_end_tx_phase(call, "ETA");
491                 return;
492         }
493
494         if (before(hard_ack, call->tx_hard_ack) ||
495             after(hard_ack, call->tx_top))
496                 return rxrpc_proto_abort("AKW", call, 0);
497
498         if (after(hard_ack, call->tx_hard_ack))
499                 rxrpc_rotate_tx_window(call, hard_ack);
500
501         if (after(first_soft_ack, call->tx_top))
502                 return;
503
504         if (nr_acks > call->tx_top - first_soft_ack + 1)
505                 nr_acks = first_soft_ack - call->tx_top + 1;
506         if (skb_copy_bits(skb, sp->offset, buf.acks, nr_acks) < 0)
507                 return rxrpc_proto_abort("XSA", call, 0);
508         rxrpc_input_soft_acks(call, buf.acks, first_soft_ack, nr_acks);
509 }
510
511 /*
512  * Process an ACKALL packet.
513  */
514 static void rxrpc_input_ackall(struct rxrpc_call *call, struct sk_buff *skb)
515 {
516         struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
517
518         _proto("Rx ACKALL %%%u", sp->hdr.serial);
519
520         rxrpc_end_tx_phase(call, "ETL");
521 }
522
523 /*
524  * Process an ABORT packet.
525  */
526 static void rxrpc_input_abort(struct rxrpc_call *call, struct sk_buff *skb)
527 {
528         struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
529         __be32 wtmp;
530         u32 abort_code = RX_CALL_DEAD;
531
532         _enter("");
533
534         if (skb->len >= 4 &&
535             skb_copy_bits(skb, sp->offset, &wtmp, sizeof(wtmp)) >= 0)
536                 abort_code = ntohl(wtmp);
537
538         _proto("Rx ABORT %%%u { %x }", sp->hdr.serial, abort_code);
539
540         if (rxrpc_set_call_completion(call, RXRPC_CALL_REMOTELY_ABORTED,
541                                       abort_code, ECONNABORTED))
542                 rxrpc_notify_socket(call);
543 }
544
545 /*
546  * Process an incoming call packet.
547  */
548 static void rxrpc_input_call_packet(struct rxrpc_call *call,
549                                     struct sk_buff *skb, u16 skew)
550 {
551         struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
552
553         _enter("%p,%p", call, skb);
554
555         switch (sp->hdr.type) {
556         case RXRPC_PACKET_TYPE_DATA:
557                 rxrpc_input_data(call, skb, skew);
558                 break;
559
560         case RXRPC_PACKET_TYPE_ACK:
561                 rxrpc_input_ack(call, skb, skew);
562                 break;
563
564         case RXRPC_PACKET_TYPE_BUSY:
565                 _proto("Rx BUSY %%%u", sp->hdr.serial);
566
567                 /* Just ignore BUSY packets from the server; the retry and
568                  * lifespan timers will take care of business.  BUSY packets
569                  * from the client don't make sense.
570                  */
571                 break;
572
573         case RXRPC_PACKET_TYPE_ABORT:
574                 rxrpc_input_abort(call, skb);
575                 break;
576
577         case RXRPC_PACKET_TYPE_ACKALL:
578                 rxrpc_input_ackall(call, skb);
579                 break;
580
581         default:
582                 _proto("Rx %s %%%u", rxrpc_pkts[sp->hdr.type], sp->hdr.serial);
583                 break;
584         }
585
586         _leave("");
587 }
588
589 /*
590  * post connection-level events to the connection
591  * - this includes challenges, responses, some aborts and call terminal packet
592  *   retransmission.
593  */
594 static void rxrpc_post_packet_to_conn(struct rxrpc_connection *conn,
595                                       struct sk_buff *skb)
596 {
597         _enter("%p,%p", conn, skb);
598
599         skb_queue_tail(&conn->rx_queue, skb);
600         rxrpc_queue_conn(conn);
601 }
602
603 /*
604  * post endpoint-level events to the local endpoint
605  * - this includes debug and version messages
606  */
607 static void rxrpc_post_packet_to_local(struct rxrpc_local *local,
608                                        struct sk_buff *skb)
609 {
610         _enter("%p,%p", local, skb);
611
612         skb_queue_tail(&local->event_queue, skb);
613         rxrpc_queue_local(local);
614 }
615
616 /*
617  * put a packet up for transport-level abort
618  */
619 static void rxrpc_reject_packet(struct rxrpc_local *local, struct sk_buff *skb)
620 {
621         CHECK_SLAB_OKAY(&local->usage);
622
623         skb_queue_tail(&local->reject_queue, skb);
624         rxrpc_queue_local(local);
625 }
626
627 /*
628  * Extract the wire header from a packet and translate the byte order.
629  */
630 static noinline
631 int rxrpc_extract_header(struct rxrpc_skb_priv *sp, struct sk_buff *skb)
632 {
633         struct rxrpc_wire_header whdr;
634
635         /* dig out the RxRPC connection details */
636         if (skb_copy_bits(skb, 0, &whdr, sizeof(whdr)) < 0)
637                 return -EBADMSG;
638
639         memset(sp, 0, sizeof(*sp));
640         sp->hdr.epoch           = ntohl(whdr.epoch);
641         sp->hdr.cid             = ntohl(whdr.cid);
642         sp->hdr.callNumber      = ntohl(whdr.callNumber);
643         sp->hdr.seq             = ntohl(whdr.seq);
644         sp->hdr.serial          = ntohl(whdr.serial);
645         sp->hdr.flags           = whdr.flags;
646         sp->hdr.type            = whdr.type;
647         sp->hdr.userStatus      = whdr.userStatus;
648         sp->hdr.securityIndex   = whdr.securityIndex;
649         sp->hdr._rsvd           = ntohs(whdr._rsvd);
650         sp->hdr.serviceId       = ntohs(whdr.serviceId);
651         sp->offset = sizeof(whdr);
652         return 0;
653 }
654
655 /*
656  * handle data received on the local endpoint
657  * - may be called in interrupt context
658  *
659  * The socket is locked by the caller and this prevents the socket from being
660  * shut down and the local endpoint from going away, thus sk_user_data will not
661  * be cleared until this function returns.
662  */
663 void rxrpc_data_ready(struct sock *udp_sk)
664 {
665         struct rxrpc_connection *conn;
666         struct rxrpc_channel *chan;
667         struct rxrpc_call *call;
668         struct rxrpc_skb_priv *sp;
669         struct rxrpc_local *local = udp_sk->sk_user_data;
670         struct sk_buff *skb;
671         unsigned int channel;
672         int ret, skew;
673
674         _enter("%p", udp_sk);
675
676         ASSERT(!irqs_disabled());
677
678         skb = skb_recv_datagram(udp_sk, 0, 1, &ret);
679         if (!skb) {
680                 if (ret == -EAGAIN)
681                         return;
682                 _debug("UDP socket error %d", ret);
683                 return;
684         }
685
686         rxrpc_new_skb(skb);
687
688         _net("recv skb %p", skb);
689
690         /* we'll probably need to checksum it (didn't call sock_recvmsg) */
691         if (skb_checksum_complete(skb)) {
692                 rxrpc_free_skb(skb);
693                 __UDP_INC_STATS(&init_net, UDP_MIB_INERRORS, 0);
694                 _leave(" [CSUM failed]");
695                 return;
696         }
697
698         __UDP_INC_STATS(&init_net, UDP_MIB_INDATAGRAMS, 0);
699
700         /* The socket buffer we have is owned by UDP, with UDP's data all over
701          * it, but we really want our own data there.
702          */
703         skb_orphan(skb);
704         sp = rxrpc_skb(skb);
705
706         _net("Rx UDP packet from %08x:%04hu",
707              ntohl(ip_hdr(skb)->saddr), ntohs(udp_hdr(skb)->source));
708
709         /* dig out the RxRPC connection details */
710         if (rxrpc_extract_header(sp, skb) < 0)
711                 goto bad_message;
712         trace_rxrpc_rx_packet(sp);
713
714         _net("Rx RxRPC %s ep=%x call=%x:%x",
715              sp->hdr.flags & RXRPC_CLIENT_INITIATED ? "ToServer" : "ToClient",
716              sp->hdr.epoch, sp->hdr.cid, sp->hdr.callNumber);
717
718         if (sp->hdr.type >= RXRPC_N_PACKET_TYPES ||
719             !((RXRPC_SUPPORTED_PACKET_TYPES >> sp->hdr.type) & 1)) {
720                 _proto("Rx Bad Packet Type %u", sp->hdr.type);
721                 goto bad_message;
722         }
723
724         switch (sp->hdr.type) {
725         case RXRPC_PACKET_TYPE_VERSION:
726                 rxrpc_post_packet_to_local(local, skb);
727                 goto out;
728
729         case RXRPC_PACKET_TYPE_BUSY:
730                 if (sp->hdr.flags & RXRPC_CLIENT_INITIATED)
731                         goto discard;
732
733         case RXRPC_PACKET_TYPE_DATA:
734                 if (sp->hdr.callNumber == 0)
735                         goto bad_message;
736                 if (sp->hdr.flags & RXRPC_JUMBO_PACKET &&
737                     !rxrpc_validate_jumbo(skb))
738                         goto bad_message;
739                 break;
740         }
741
742         rcu_read_lock();
743
744         conn = rxrpc_find_connection_rcu(local, skb);
745         if (conn) {
746                 if (sp->hdr.securityIndex != conn->security_ix)
747                         goto wrong_security;
748
749                 if (sp->hdr.callNumber == 0) {
750                         /* Connection-level packet */
751                         _debug("CONN %p {%d}", conn, conn->debug_id);
752                         rxrpc_post_packet_to_conn(conn, skb);
753                         goto out_unlock;
754                 }
755
756                 /* Note the serial number skew here */
757                 skew = (int)sp->hdr.serial - (int)conn->hi_serial;
758                 if (skew >= 0) {
759                         if (skew > 0)
760                                 conn->hi_serial = sp->hdr.serial;
761                 } else {
762                         skew = -skew;
763                         skew = min(skew, 65535);
764                 }
765
766                 /* Call-bound packets are routed by connection channel. */
767                 channel = sp->hdr.cid & RXRPC_CHANNELMASK;
768                 chan = &conn->channels[channel];
769
770                 /* Ignore really old calls */
771                 if (sp->hdr.callNumber < chan->last_call)
772                         goto discard_unlock;
773
774                 if (sp->hdr.callNumber == chan->last_call) {
775                         /* For the previous service call, if completed successfully, we
776                          * discard all further packets.
777                          */
778                         if (rxrpc_conn_is_service(conn) &&
779                             (chan->last_type == RXRPC_PACKET_TYPE_ACK ||
780                              sp->hdr.type == RXRPC_PACKET_TYPE_ABORT))
781                                 goto discard_unlock;
782
783                         /* But otherwise we need to retransmit the final packet from
784                          * data cached in the connection record.
785                          */
786                         rxrpc_post_packet_to_conn(conn, skb);
787                         goto out_unlock;
788                 }
789
790                 call = rcu_dereference(chan->call);
791         } else {
792                 skew = 0;
793                 call = NULL;
794         }
795
796         if (!call || atomic_read(&call->usage) == 0) {
797                 if (!(sp->hdr.type & RXRPC_CLIENT_INITIATED) ||
798                     sp->hdr.callNumber == 0 ||
799                     sp->hdr.type != RXRPC_PACKET_TYPE_DATA)
800                         goto bad_message_unlock;
801                 if (sp->hdr.seq != 1)
802                         goto discard_unlock;
803                 call = rxrpc_new_incoming_call(local, conn, skb);
804                 if (!call) {
805                         rcu_read_unlock();
806                         goto reject_packet;
807                 }
808         }
809
810         rxrpc_input_call_packet(call, skb, skew);
811         goto discard_unlock;
812
813 discard_unlock:
814         rcu_read_unlock();
815 discard:
816         rxrpc_free_skb(skb);
817 out:
818         trace_rxrpc_rx_done(0, 0);
819         return;
820
821 out_unlock:
822         rcu_read_unlock();
823         goto out;
824
825 wrong_security:
826         rcu_read_unlock();
827         trace_rxrpc_abort("SEC", sp->hdr.cid, sp->hdr.callNumber, sp->hdr.seq,
828                           RXKADINCONSISTENCY, EBADMSG);
829         skb->priority = RXKADINCONSISTENCY;
830         goto post_abort;
831
832 bad_message_unlock:
833         rcu_read_unlock();
834 bad_message:
835         trace_rxrpc_abort("BAD", sp->hdr.cid, sp->hdr.callNumber, sp->hdr.seq,
836                           RX_PROTOCOL_ERROR, EBADMSG);
837         skb->priority = RX_PROTOCOL_ERROR;
838 post_abort:
839         skb->mark = RXRPC_SKB_MARK_LOCAL_ABORT;
840 reject_packet:
841         trace_rxrpc_rx_done(skb->mark, skb->priority);
842         rxrpc_reject_packet(local, skb);
843         _leave(" [badmsg]");
844 }