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