af49c2992c4aeb06b8ac2bf16b1c0f3e214332d9
[linux-block.git] / net / rxrpc / input.c
1 /* RxRPC packet reception
2  *
3  * Copyright (C) 2007 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 /*
31  * queue a packet for recvmsg to pass to userspace
32  * - the caller must hold a lock on call->lock
33  * - must not be called with interrupts disabled (sk_filter() disables BH's)
34  * - eats the packet whether successful or not
35  * - there must be just one reference to the packet, which the caller passes to
36  *   this function
37  */
38 int rxrpc_queue_rcv_skb(struct rxrpc_call *call, struct sk_buff *skb,
39                         bool force, bool terminal)
40 {
41         struct rxrpc_skb_priv *sp;
42         struct rxrpc_sock *rx = call->socket;
43         struct sock *sk;
44         int ret;
45
46         _enter(",,%d,%d", force, terminal);
47
48         ASSERT(!irqs_disabled());
49
50         sp = rxrpc_skb(skb);
51         ASSERTCMP(sp->call, ==, call);
52
53         /* if we've already posted the terminal message for a call, then we
54          * don't post any more */
55         if (test_bit(RXRPC_CALL_TERMINAL_MSG, &call->flags)) {
56                 _debug("already terminated");
57                 ASSERTCMP(call->state, >=, RXRPC_CALL_COMPLETE);
58                 rxrpc_free_skb(skb);
59                 return 0;
60         }
61
62         sk = &rx->sk;
63
64         if (!force) {
65                 /* cast skb->rcvbuf to unsigned...  It's pointless, but
66                  * reduces number of warnings when compiling with -W
67                  * --ANK */
68 //              ret = -ENOBUFS;
69 //              if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >=
70 //                  (unsigned int) sk->sk_rcvbuf)
71 //                      goto out;
72
73                 ret = sk_filter(sk, skb);
74                 if (ret < 0)
75                         goto out;
76         }
77
78         spin_lock_bh(&sk->sk_receive_queue.lock);
79         if (!test_bit(RXRPC_CALL_TERMINAL_MSG, &call->flags) &&
80             !test_bit(RXRPC_CALL_RELEASED, &call->flags) &&
81             call->socket->sk.sk_state != RXRPC_CLOSE) {
82                 skb->destructor = rxrpc_packet_destructor;
83                 skb->dev = NULL;
84                 skb->sk = sk;
85                 atomic_add(skb->truesize, &sk->sk_rmem_alloc);
86
87                 if (terminal) {
88                         _debug("<<<< TERMINAL MESSAGE >>>>");
89                         set_bit(RXRPC_CALL_TERMINAL_MSG, &call->flags);
90                 }
91
92                 /* allow interception by a kernel service */
93                 if (rx->interceptor) {
94                         rx->interceptor(sk, call->user_call_ID, skb);
95                         spin_unlock_bh(&sk->sk_receive_queue.lock);
96                 } else {
97                         _net("post skb %p", skb);
98                         __skb_queue_tail(&sk->sk_receive_queue, skb);
99                         spin_unlock_bh(&sk->sk_receive_queue.lock);
100
101                         if (!sock_flag(sk, SOCK_DEAD))
102                                 sk->sk_data_ready(sk);
103                 }
104                 skb = NULL;
105         } else {
106                 spin_unlock_bh(&sk->sk_receive_queue.lock);
107         }
108         ret = 0;
109
110 out:
111         rxrpc_free_skb(skb);
112
113         _leave(" = %d", ret);
114         return ret;
115 }
116
117 /*
118  * process a DATA packet, posting the packet to the appropriate queue
119  * - eats the packet if successful
120  */
121 static int rxrpc_fast_process_data(struct rxrpc_call *call,
122                                    struct sk_buff *skb, u32 seq)
123 {
124         struct rxrpc_skb_priv *sp;
125         bool terminal;
126         int ret, ackbit, ack;
127         u32 serial;
128         u16 skew;
129         u8 flags;
130
131         _enter("{%u,%u},,{%u}", call->rx_data_post, call->rx_first_oos, seq);
132
133         sp = rxrpc_skb(skb);
134         ASSERTCMP(sp->call, ==, NULL);
135         flags = sp->hdr.flags;
136         serial = sp->hdr.serial;
137         skew = skb->priority;
138
139         spin_lock(&call->lock);
140
141         if (call->state > RXRPC_CALL_COMPLETE)
142                 goto discard;
143
144         ASSERTCMP(call->rx_data_expect, >=, call->rx_data_post);
145         ASSERTCMP(call->rx_data_post, >=, call->rx_data_recv);
146         ASSERTCMP(call->rx_data_recv, >=, call->rx_data_eaten);
147
148         if (seq < call->rx_data_post) {
149                 _debug("dup #%u [-%u]", seq, call->rx_data_post);
150                 ack = RXRPC_ACK_DUPLICATE;
151                 ret = -ENOBUFS;
152                 goto discard_and_ack;
153         }
154
155         /* we may already have the packet in the out of sequence queue */
156         ackbit = seq - (call->rx_data_eaten + 1);
157         ASSERTCMP(ackbit, >=, 0);
158         if (__test_and_set_bit(ackbit, call->ackr_window)) {
159                 _debug("dup oos #%u [%u,%u]",
160                        seq, call->rx_data_eaten, call->rx_data_post);
161                 ack = RXRPC_ACK_DUPLICATE;
162                 goto discard_and_ack;
163         }
164
165         if (seq >= call->ackr_win_top) {
166                 _debug("exceed #%u [%u]", seq, call->ackr_win_top);
167                 __clear_bit(ackbit, call->ackr_window);
168                 ack = RXRPC_ACK_EXCEEDS_WINDOW;
169                 goto discard_and_ack;
170         }
171
172         if (seq == call->rx_data_expect) {
173                 clear_bit(RXRPC_CALL_EXPECT_OOS, &call->flags);
174                 call->rx_data_expect++;
175         } else if (seq > call->rx_data_expect) {
176                 _debug("oos #%u [%u]", seq, call->rx_data_expect);
177                 call->rx_data_expect = seq + 1;
178                 if (test_and_set_bit(RXRPC_CALL_EXPECT_OOS, &call->flags)) {
179                         ack = RXRPC_ACK_OUT_OF_SEQUENCE;
180                         goto enqueue_and_ack;
181                 }
182                 goto enqueue_packet;
183         }
184
185         if (seq != call->rx_data_post) {
186                 _debug("ahead #%u [%u]", seq, call->rx_data_post);
187                 goto enqueue_packet;
188         }
189
190         if (test_bit(RXRPC_CALL_RCVD_LAST, &call->flags))
191                 goto protocol_error;
192
193         /* if the packet need security things doing to it, then it goes down
194          * the slow path */
195         if (call->conn->security_ix)
196                 goto enqueue_packet;
197
198         sp->call = call;
199         rxrpc_get_call(call);
200         atomic_inc(&call->skb_count);
201         terminal = ((flags & RXRPC_LAST_PACKET) &&
202                     !(flags & RXRPC_CLIENT_INITIATED));
203         ret = rxrpc_queue_rcv_skb(call, skb, false, terminal);
204         if (ret < 0) {
205                 if (ret == -ENOMEM || ret == -ENOBUFS) {
206                         __clear_bit(ackbit, call->ackr_window);
207                         ack = RXRPC_ACK_NOSPACE;
208                         goto discard_and_ack;
209                 }
210                 goto out;
211         }
212
213         skb = NULL;
214         sp = NULL;
215
216         _debug("post #%u", seq);
217         ASSERTCMP(call->rx_data_post, ==, seq);
218         call->rx_data_post++;
219
220         if (flags & RXRPC_LAST_PACKET)
221                 set_bit(RXRPC_CALL_RCVD_LAST, &call->flags);
222
223         /* if we've reached an out of sequence packet then we need to drain
224          * that queue into the socket Rx queue now */
225         if (call->rx_data_post == call->rx_first_oos) {
226                 _debug("drain rx oos now");
227                 read_lock(&call->state_lock);
228                 if (call->state < RXRPC_CALL_COMPLETE &&
229                     !test_and_set_bit(RXRPC_CALL_EV_DRAIN_RX_OOS, &call->events))
230                         rxrpc_queue_call(call);
231                 read_unlock(&call->state_lock);
232         }
233
234         spin_unlock(&call->lock);
235         atomic_inc(&call->ackr_not_idle);
236         rxrpc_propose_ACK(call, RXRPC_ACK_DELAY, skew, serial, false);
237         _leave(" = 0 [posted]");
238         return 0;
239
240 protocol_error:
241         ret = -EBADMSG;
242 out:
243         spin_unlock(&call->lock);
244         _leave(" = %d", ret);
245         return ret;
246
247 discard_and_ack:
248         _debug("discard and ACK packet %p", skb);
249         __rxrpc_propose_ACK(call, ack, skew, serial, true);
250 discard:
251         spin_unlock(&call->lock);
252         rxrpc_free_skb(skb);
253         _leave(" = 0 [discarded]");
254         return 0;
255
256 enqueue_and_ack:
257         __rxrpc_propose_ACK(call, ack, skew, serial, true);
258 enqueue_packet:
259         _net("defer skb %p", skb);
260         spin_unlock(&call->lock);
261         skb_queue_tail(&call->rx_queue, skb);
262         atomic_inc(&call->ackr_not_idle);
263         read_lock(&call->state_lock);
264         if (call->state < RXRPC_CALL_DEAD)
265                 rxrpc_queue_call(call);
266         read_unlock(&call->state_lock);
267         _leave(" = 0 [queued]");
268         return 0;
269 }
270
271 /*
272  * assume an implicit ACKALL of the transmission phase of a client socket upon
273  * reception of the first reply packet
274  */
275 static void rxrpc_assume_implicit_ackall(struct rxrpc_call *call, u32 serial)
276 {
277         write_lock_bh(&call->state_lock);
278
279         switch (call->state) {
280         case RXRPC_CALL_CLIENT_AWAIT_REPLY:
281                 call->state = RXRPC_CALL_CLIENT_RECV_REPLY;
282                 call->acks_latest = serial;
283
284                 _debug("implicit ACKALL %%%u", call->acks_latest);
285                 set_bit(RXRPC_CALL_EV_RCVD_ACKALL, &call->events);
286                 write_unlock_bh(&call->state_lock);
287
288                 if (try_to_del_timer_sync(&call->resend_timer) >= 0) {
289                         clear_bit(RXRPC_CALL_EV_RESEND_TIMER, &call->events);
290                         clear_bit(RXRPC_CALL_EV_RESEND, &call->events);
291                         clear_bit(RXRPC_CALL_RUN_RTIMER, &call->flags);
292                 }
293                 break;
294
295         default:
296                 write_unlock_bh(&call->state_lock);
297                 break;
298         }
299 }
300
301 /*
302  * post an incoming packet to the nominated call to deal with
303  * - must get rid of the sk_buff, either by freeing it or by queuing it
304  */
305 void rxrpc_fast_process_packet(struct rxrpc_call *call, struct sk_buff *skb)
306 {
307         struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
308         __be32 wtmp;
309         u32 abort_code;
310
311         _enter("%p,%p", call, skb);
312
313         ASSERT(!irqs_disabled());
314
315 #if 0 // INJECT RX ERROR
316         if (sp->hdr.type == RXRPC_PACKET_TYPE_DATA) {
317                 static int skip = 0;
318                 if (++skip == 3) {
319                         printk("DROPPED 3RD PACKET!!!!!!!!!!!!!\n");
320                         skip = 0;
321                         goto free_packet;
322                 }
323         }
324 #endif
325
326         /* request ACK generation for any ACK or DATA packet that requests
327          * it */
328         if (sp->hdr.flags & RXRPC_REQUEST_ACK) {
329                 _proto("ACK Requested on %%%u", sp->hdr.serial);
330                 rxrpc_propose_ACK(call, RXRPC_ACK_REQUESTED,
331                                   skb->priority, sp->hdr.serial, false);
332         }
333
334         switch (sp->hdr.type) {
335         case RXRPC_PACKET_TYPE_ABORT:
336                 _debug("abort");
337
338                 if (skb_copy_bits(skb, 0, &wtmp, sizeof(wtmp)) < 0)
339                         goto protocol_error;
340
341                 abort_code = ntohl(wtmp);
342                 _proto("Rx ABORT %%%u { %x }", sp->hdr.serial, abort_code);
343
344                 if (__rxrpc_set_call_completion(call,
345                                                 RXRPC_CALL_REMOTELY_ABORTED,
346                                                 abort_code, ECONNABORTED)) {
347                         set_bit(RXRPC_CALL_EV_RCVD_ABORT, &call->events);
348                         rxrpc_queue_call(call);
349                 }
350                 goto free_packet;
351
352         case RXRPC_PACKET_TYPE_BUSY:
353                 _proto("Rx BUSY %%%u", sp->hdr.serial);
354
355                 if (rxrpc_conn_is_service(call->conn))
356                         goto protocol_error;
357
358                 write_lock_bh(&call->state_lock);
359                 switch (call->state) {
360                 case RXRPC_CALL_CLIENT_SEND_REQUEST:
361                         __rxrpc_set_call_completion(call,
362                                                     RXRPC_CALL_SERVER_BUSY,
363                                                     0, EBUSY);
364                         set_bit(RXRPC_CALL_EV_RCVD_BUSY, &call->events);
365                         rxrpc_queue_call(call);
366                 case RXRPC_CALL_SERVER_BUSY:
367                         goto free_packet_unlock;
368                 default:
369                         goto protocol_error_locked;
370                 }
371
372         default:
373                 _proto("Rx %s %%%u", rxrpc_pkts[sp->hdr.type], sp->hdr.serial);
374                 goto protocol_error;
375
376         case RXRPC_PACKET_TYPE_DATA:
377                 _proto("Rx DATA %%%u { #%u }", sp->hdr.serial, sp->hdr.seq);
378
379                 if (sp->hdr.seq == 0)
380                         goto protocol_error;
381
382                 call->ackr_prev_seq = sp->hdr.seq;
383
384                 /* received data implicitly ACKs all of the request packets we
385                  * sent when we're acting as a client */
386                 if (call->state == RXRPC_CALL_CLIENT_AWAIT_REPLY)
387                         rxrpc_assume_implicit_ackall(call, sp->hdr.serial);
388
389                 switch (rxrpc_fast_process_data(call, skb, sp->hdr.seq)) {
390                 case 0:
391                         skb = NULL;
392                         goto done;
393
394                 default:
395                         BUG();
396
397                         /* data packet received beyond the last packet */
398                 case -EBADMSG:
399                         goto protocol_error;
400                 }
401
402         case RXRPC_PACKET_TYPE_ACKALL:
403         case RXRPC_PACKET_TYPE_ACK:
404                 /* ACK processing is done in process context */
405                 read_lock_bh(&call->state_lock);
406                 if (call->state < RXRPC_CALL_DEAD) {
407                         skb_queue_tail(&call->rx_queue, skb);
408                         rxrpc_queue_call(call);
409                         skb = NULL;
410                 }
411                 read_unlock_bh(&call->state_lock);
412                 goto free_packet;
413         }
414
415 protocol_error:
416         _debug("protocol error");
417         write_lock_bh(&call->state_lock);
418 protocol_error_locked:
419         if (__rxrpc_abort_call(call, RX_PROTOCOL_ERROR, EPROTO))
420                 rxrpc_queue_call(call);
421 free_packet_unlock:
422         write_unlock_bh(&call->state_lock);
423 free_packet:
424         rxrpc_free_skb(skb);
425 done:
426         _leave("");
427 }
428
429 /*
430  * split up a jumbo data packet
431  */
432 static void rxrpc_process_jumbo_packet(struct rxrpc_call *call,
433                                        struct sk_buff *jumbo)
434 {
435         struct rxrpc_jumbo_header jhdr;
436         struct rxrpc_skb_priv *sp;
437         struct sk_buff *part;
438
439         _enter(",{%u,%u}", jumbo->data_len, jumbo->len);
440
441         sp = rxrpc_skb(jumbo);
442
443         do {
444                 sp->hdr.flags &= ~RXRPC_JUMBO_PACKET;
445
446                 /* make a clone to represent the first subpacket in what's left
447                  * of the jumbo packet */
448                 part = skb_clone(jumbo, GFP_ATOMIC);
449                 if (!part) {
450                         /* simply ditch the tail in the event of ENOMEM */
451                         pskb_trim(jumbo, RXRPC_JUMBO_DATALEN);
452                         break;
453                 }
454                 rxrpc_new_skb(part);
455
456                 pskb_trim(part, RXRPC_JUMBO_DATALEN);
457
458                 if (!pskb_pull(jumbo, RXRPC_JUMBO_DATALEN))
459                         goto protocol_error;
460
461                 if (skb_copy_bits(jumbo, 0, &jhdr, sizeof(jhdr)) < 0)
462                         goto protocol_error;
463                 if (!pskb_pull(jumbo, sizeof(jhdr)))
464                         BUG();
465
466                 sp->hdr.seq     += 1;
467                 sp->hdr.serial  += 1;
468                 sp->hdr.flags   = jhdr.flags;
469                 sp->hdr._rsvd   = ntohs(jhdr._rsvd);
470
471                 _proto("Rx DATA Jumbo %%%u", sp->hdr.serial - 1);
472
473                 rxrpc_fast_process_packet(call, part);
474                 part = NULL;
475
476         } while (sp->hdr.flags & RXRPC_JUMBO_PACKET);
477
478         rxrpc_fast_process_packet(call, jumbo);
479         _leave("");
480         return;
481
482 protocol_error:
483         _debug("protocol error");
484         rxrpc_free_skb(part);
485         rxrpc_free_skb(jumbo);
486         if (rxrpc_abort_call(call, RX_PROTOCOL_ERROR, EPROTO))
487                 rxrpc_queue_call(call);
488         _leave("");
489 }
490
491 /*
492  * post an incoming packet to the appropriate call/socket to deal with
493  * - must get rid of the sk_buff, either by freeing it or by queuing it
494  */
495 static void rxrpc_post_packet_to_call(struct rxrpc_call *call,
496                                       struct sk_buff *skb)
497 {
498         struct rxrpc_skb_priv *sp;
499
500         _enter("%p,%p", call, skb);
501
502         sp = rxrpc_skb(skb);
503
504         _debug("extant call [%d]", call->state);
505
506         read_lock(&call->state_lock);
507         switch (call->state) {
508         case RXRPC_CALL_DEAD:
509                 goto dead_call;
510
511         case RXRPC_CALL_COMPLETE:
512                 switch (call->completion) {
513                 case RXRPC_CALL_LOCALLY_ABORTED:
514                         if (!test_and_set_bit(RXRPC_CALL_EV_ABORT,
515                                               &call->events)) {
516                                 rxrpc_queue_call(call);
517                                 goto free_unlock;
518                         }
519                 default:
520                         goto dead_call;
521                 case RXRPC_CALL_SUCCEEDED:
522                         if (rxrpc_conn_is_service(call->conn))
523                                 goto dead_call;
524                         goto resend_final_ack;
525                 }
526
527         case RXRPC_CALL_CLIENT_FINAL_ACK:
528                 goto resend_final_ack;
529
530         default:
531                 break;
532         }
533
534         read_unlock(&call->state_lock);
535         rxrpc_get_call(call);
536
537         if (sp->hdr.type == RXRPC_PACKET_TYPE_DATA &&
538             sp->hdr.flags & RXRPC_JUMBO_PACKET)
539                 rxrpc_process_jumbo_packet(call, skb);
540         else
541                 rxrpc_fast_process_packet(call, skb);
542
543         rxrpc_put_call(call);
544         goto done;
545
546 resend_final_ack:
547         _debug("final ack again");
548         rxrpc_get_call(call);
549         set_bit(RXRPC_CALL_EV_ACK_FINAL, &call->events);
550         rxrpc_queue_call(call);
551         goto free_unlock;
552
553 dead_call:
554         if (sp->hdr.type != RXRPC_PACKET_TYPE_ABORT) {
555                 skb->priority = RX_CALL_DEAD;
556                 rxrpc_reject_packet(call->conn->params.local, skb);
557                 goto unlock;
558         }
559 free_unlock:
560         rxrpc_free_skb(skb);
561 unlock:
562         read_unlock(&call->state_lock);
563 done:
564         _leave("");
565 }
566
567 /*
568  * post connection-level events to the connection
569  * - this includes challenges, responses, some aborts and call terminal packet
570  *   retransmission.
571  */
572 static void rxrpc_post_packet_to_conn(struct rxrpc_connection *conn,
573                                       struct sk_buff *skb)
574 {
575         _enter("%p,%p", conn, skb);
576
577         skb_queue_tail(&conn->rx_queue, skb);
578         rxrpc_queue_conn(conn);
579 }
580
581 /*
582  * post endpoint-level events to the local endpoint
583  * - this includes debug and version messages
584  */
585 static void rxrpc_post_packet_to_local(struct rxrpc_local *local,
586                                        struct sk_buff *skb)
587 {
588         _enter("%p,%p", local, skb);
589
590         skb_queue_tail(&local->event_queue, skb);
591         rxrpc_queue_local(local);
592 }
593
594 /*
595  * Extract the wire header from a packet and translate the byte order.
596  */
597 static noinline
598 int rxrpc_extract_header(struct rxrpc_skb_priv *sp, struct sk_buff *skb)
599 {
600         struct rxrpc_wire_header whdr;
601
602         /* dig out the RxRPC connection details */
603         if (skb_copy_bits(skb, 0, &whdr, sizeof(whdr)) < 0)
604                 return -EBADMSG;
605         if (!pskb_pull(skb, sizeof(whdr)))
606                 BUG();
607
608         memset(sp, 0, sizeof(*sp));
609         sp->hdr.epoch           = ntohl(whdr.epoch);
610         sp->hdr.cid             = ntohl(whdr.cid);
611         sp->hdr.callNumber      = ntohl(whdr.callNumber);
612         sp->hdr.seq             = ntohl(whdr.seq);
613         sp->hdr.serial          = ntohl(whdr.serial);
614         sp->hdr.flags           = whdr.flags;
615         sp->hdr.type            = whdr.type;
616         sp->hdr.userStatus      = whdr.userStatus;
617         sp->hdr.securityIndex   = whdr.securityIndex;
618         sp->hdr._rsvd           = ntohs(whdr._rsvd);
619         sp->hdr.serviceId       = ntohs(whdr.serviceId);
620         return 0;
621 }
622
623 /*
624  * handle data received on the local endpoint
625  * - may be called in interrupt context
626  *
627  * The socket is locked by the caller and this prevents the socket from being
628  * shut down and the local endpoint from going away, thus sk_user_data will not
629  * be cleared until this function returns.
630  */
631 void rxrpc_data_ready(struct sock *sk)
632 {
633         struct rxrpc_connection *conn;
634         struct rxrpc_skb_priv *sp;
635         struct rxrpc_local *local = sk->sk_user_data;
636         struct sk_buff *skb;
637         int ret, skew;
638
639         _enter("%p", sk);
640
641         ASSERT(!irqs_disabled());
642
643         skb = skb_recv_datagram(sk, 0, 1, &ret);
644         if (!skb) {
645                 if (ret == -EAGAIN)
646                         return;
647                 _debug("UDP socket error %d", ret);
648                 return;
649         }
650
651         rxrpc_new_skb(skb);
652
653         _net("recv skb %p", skb);
654
655         /* we'll probably need to checksum it (didn't call sock_recvmsg) */
656         if (skb_checksum_complete(skb)) {
657                 rxrpc_free_skb(skb);
658                 __UDP_INC_STATS(&init_net, UDP_MIB_INERRORS, 0);
659                 _leave(" [CSUM failed]");
660                 return;
661         }
662
663         __UDP_INC_STATS(&init_net, UDP_MIB_INDATAGRAMS, 0);
664
665         /* The socket buffer we have is owned by UDP, with UDP's data all over
666          * it, but we really want our own data there.
667          */
668         skb_orphan(skb);
669         sp = rxrpc_skb(skb);
670
671         _net("Rx UDP packet from %08x:%04hu",
672              ntohl(ip_hdr(skb)->saddr), ntohs(udp_hdr(skb)->source));
673
674         /* dig out the RxRPC connection details */
675         if (rxrpc_extract_header(sp, skb) < 0)
676                 goto bad_message;
677
678         _net("Rx RxRPC %s ep=%x call=%x:%x",
679              sp->hdr.flags & RXRPC_CLIENT_INITIATED ? "ToServer" : "ToClient",
680              sp->hdr.epoch, sp->hdr.cid, sp->hdr.callNumber);
681
682         if (sp->hdr.type >= RXRPC_N_PACKET_TYPES ||
683             !((RXRPC_SUPPORTED_PACKET_TYPES >> sp->hdr.type) & 1)) {
684                 _proto("Rx Bad Packet Type %u", sp->hdr.type);
685                 goto bad_message;
686         }
687
688         if (sp->hdr.type == RXRPC_PACKET_TYPE_VERSION) {
689                 rxrpc_post_packet_to_local(local, skb);
690                 goto out;
691         }
692
693         if (sp->hdr.type == RXRPC_PACKET_TYPE_DATA &&
694             (sp->hdr.callNumber == 0 || sp->hdr.seq == 0))
695                 goto bad_message;
696
697         rcu_read_lock();
698
699         conn = rxrpc_find_connection_rcu(local, skb);
700         if (!conn) {
701                 skb->priority = 0;
702                 goto cant_route_call;
703         }
704
705         /* Note the serial number skew here */
706         skew = (int)sp->hdr.serial - (int)conn->hi_serial;
707         if (skew >= 0) {
708                 if (skew > 0)
709                         conn->hi_serial = sp->hdr.serial;
710                 skb->priority = 0;
711         } else {
712                 skew = -skew;
713                 skb->priority = min(skew, 65535);
714         }
715
716         if (sp->hdr.callNumber == 0) {
717                 /* Connection-level packet */
718                 _debug("CONN %p {%d}", conn, conn->debug_id);
719                 rxrpc_post_packet_to_conn(conn, skb);
720                 goto out_unlock;
721         } else {
722                 /* Call-bound packets are routed by connection channel. */
723                 unsigned int channel = sp->hdr.cid & RXRPC_CHANNELMASK;
724                 struct rxrpc_channel *chan = &conn->channels[channel];
725                 struct rxrpc_call *call;
726
727                 /* Ignore really old calls */
728                 if (sp->hdr.callNumber < chan->last_call)
729                         goto discard_unlock;
730
731                 if (sp->hdr.callNumber == chan->last_call) {
732                         /* For the previous service call, if completed
733                          * successfully, we discard all further packets.
734                          */
735                         if (rxrpc_conn_is_service(conn) &&
736                             (chan->last_type == RXRPC_PACKET_TYPE_ACK ||
737                              sp->hdr.type == RXRPC_PACKET_TYPE_ABORT))
738                                 goto discard_unlock;
739
740                         /* But otherwise we need to retransmit the final packet
741                          * from data cached in the connection record.
742                          */
743                         rxrpc_post_packet_to_conn(conn, skb);
744                         goto out_unlock;
745                 }
746
747                 call = rcu_dereference(chan->call);
748                 if (!call || atomic_read(&call->usage) == 0)
749                         goto cant_route_call;
750
751                 rxrpc_post_packet_to_call(call, skb);
752                 goto out_unlock;
753         }
754
755 discard_unlock:
756         rxrpc_free_skb(skb);
757 out_unlock:
758         rcu_read_unlock();
759 out:
760         return;
761
762 cant_route_call:
763         rcu_read_unlock();
764
765         _debug("can't route call");
766         if (sp->hdr.flags & RXRPC_CLIENT_INITIATED &&
767             sp->hdr.type == RXRPC_PACKET_TYPE_DATA) {
768                 if (sp->hdr.seq == 1) {
769                         _debug("first packet");
770                         skb_queue_tail(&local->accept_queue, skb);
771                         rxrpc_queue_work(&local->processor);
772                         _leave(" [incoming]");
773                         return;
774                 }
775                 skb->priority = RX_INVALID_OPERATION;
776         } else {
777                 skb->priority = RX_CALL_DEAD;
778         }
779
780         if (sp->hdr.type != RXRPC_PACKET_TYPE_ABORT) {
781                 _debug("reject type %d",sp->hdr.type);
782                 rxrpc_reject_packet(local, skb);
783         } else {
784                 rxrpc_free_skb(skb);
785         }
786         _leave(" [no call]");
787         return;
788
789 bad_message:
790         skb->priority = RX_PROTOCOL_ERROR;
791         rxrpc_reject_packet(local, skb);
792         _leave(" [badmsg]");
793 }