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