Merge branch 'fix/pcm-jiffies-check' into fix/asoc
[linux-2.6] / net / rxrpc / ar-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 #include <linux/module.h>
13 #include <linux/net.h>
14 #include <linux/skbuff.h>
15 #include <linux/errqueue.h>
16 #include <linux/udp.h>
17 #include <linux/in.h>
18 #include <linux/in6.h>
19 #include <linux/icmp.h>
20 #include <net/sock.h>
21 #include <net/af_rxrpc.h>
22 #include <net/ip.h>
23 #include <net/udp.h>
24 #include <net/net_namespace.h>
25 #include "ar-internal.h"
26
27 unsigned long rxrpc_ack_timeout = 1;
28
29 const char *rxrpc_pkts[] = {
30         "?00",
31         "DATA", "ACK", "BUSY", "ABORT", "ACKALL", "CHALL", "RESP", "DEBUG",
32         "?09", "?10", "?11", "?12", "?13", "?14", "?15"
33 };
34
35 /*
36  * queue a packet for recvmsg to pass to userspace
37  * - the caller must hold a lock on call->lock
38  * - must not be called with interrupts disabled (sk_filter() disables BH's)
39  * - eats the packet whether successful or not
40  * - there must be just one reference to the packet, which the caller passes to
41  *   this function
42  */
43 int rxrpc_queue_rcv_skb(struct rxrpc_call *call, struct sk_buff *skb,
44                         bool force, bool terminal)
45 {
46         struct rxrpc_skb_priv *sp;
47         struct rxrpc_sock *rx = call->socket;
48         struct sock *sk;
49         int skb_len, ret;
50
51         _enter(",,%d,%d", force, terminal);
52
53         ASSERT(!irqs_disabled());
54
55         sp = rxrpc_skb(skb);
56         ASSERTCMP(sp->call, ==, call);
57
58         /* if we've already posted the terminal message for a call, then we
59          * don't post any more */
60         if (test_bit(RXRPC_CALL_TERMINAL_MSG, &call->flags)) {
61                 _debug("already terminated");
62                 ASSERTCMP(call->state, >=, RXRPC_CALL_COMPLETE);
63                 skb->destructor = NULL;
64                 sp->call = NULL;
65                 rxrpc_put_call(call);
66                 rxrpc_free_skb(skb);
67                 return 0;
68         }
69
70         sk = &rx->sk;
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) 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             call->socket->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 (rx->interceptor) {
102                         rx->interceptor(sk, call->user_call_ID, skb);
103                         spin_unlock_bh(&sk->sk_receive_queue.lock);
104                 } else {
105
106                         /* Cache the SKB length before we tack it onto the
107                          * receive queue.  Once it is added it no longer
108                          * belongs to us and may be freed by other threads of
109                          * control pulling packets from the queue */
110                         skb_len = skb->len;
111
112                         _net("post skb %p", skb);
113                         __skb_queue_tail(&sk->sk_receive_queue, skb);
114                         spin_unlock_bh(&sk->sk_receive_queue.lock);
115
116                         if (!sock_flag(sk, SOCK_DEAD))
117                                 sk->sk_data_ready(sk, skb_len);
118                 }
119                 skb = NULL;
120         } else {
121                 spin_unlock_bh(&sk->sk_receive_queue.lock);
122         }
123         ret = 0;
124
125 out:
126         /* release the socket buffer */
127         if (skb) {
128                 skb->destructor = NULL;
129                 sp->call = NULL;
130                 rxrpc_put_call(call);
131                 rxrpc_free_skb(skb);
132         }
133
134         _leave(" = %d", ret);
135         return ret;
136 }
137
138 /*
139  * process a DATA packet, posting the packet to the appropriate queue
140  * - eats the packet if successful
141  */
142 static int rxrpc_fast_process_data(struct rxrpc_call *call,
143                                    struct sk_buff *skb, u32 seq)
144 {
145         struct rxrpc_skb_priv *sp;
146         bool terminal;
147         int ret, ackbit, ack;
148
149         _enter("{%u,%u},,{%u}", call->rx_data_post, call->rx_first_oos, seq);
150
151         sp = rxrpc_skb(skb);
152         ASSERTCMP(sp->call, ==, NULL);
153
154         spin_lock(&call->lock);
155
156         if (call->state > RXRPC_CALL_COMPLETE)
157                 goto discard;
158
159         ASSERTCMP(call->rx_data_expect, >=, call->rx_data_post);
160         ASSERTCMP(call->rx_data_post, >=, call->rx_data_recv);
161         ASSERTCMP(call->rx_data_recv, >=, call->rx_data_eaten);
162
163         if (seq < call->rx_data_post) {
164                 _debug("dup #%u [-%u]", seq, call->rx_data_post);
165                 ack = RXRPC_ACK_DUPLICATE;
166                 ret = -ENOBUFS;
167                 goto discard_and_ack;
168         }
169
170         /* we may already have the packet in the out of sequence queue */
171         ackbit = seq - (call->rx_data_eaten + 1);
172         ASSERTCMP(ackbit, >=, 0);
173         if (__test_and_set_bit(ackbit, call->ackr_window)) {
174                 _debug("dup oos #%u [%u,%u]",
175                        seq, call->rx_data_eaten, call->rx_data_post);
176                 ack = RXRPC_ACK_DUPLICATE;
177                 goto discard_and_ack;
178         }
179
180         if (seq >= call->ackr_win_top) {
181                 _debug("exceed #%u [%u]", seq, call->ackr_win_top);
182                 __clear_bit(ackbit, call->ackr_window);
183                 ack = RXRPC_ACK_EXCEEDS_WINDOW;
184                 goto discard_and_ack;
185         }
186
187         if (seq == call->rx_data_expect) {
188                 clear_bit(RXRPC_CALL_EXPECT_OOS, &call->flags);
189                 call->rx_data_expect++;
190         } else if (seq > call->rx_data_expect) {
191                 _debug("oos #%u [%u]", seq, call->rx_data_expect);
192                 call->rx_data_expect = seq + 1;
193                 if (test_and_set_bit(RXRPC_CALL_EXPECT_OOS, &call->flags)) {
194                         ack = RXRPC_ACK_OUT_OF_SEQUENCE;
195                         goto enqueue_and_ack;
196                 }
197                 goto enqueue_packet;
198         }
199
200         if (seq != call->rx_data_post) {
201                 _debug("ahead #%u [%u]", seq, call->rx_data_post);
202                 goto enqueue_packet;
203         }
204
205         if (test_bit(RXRPC_CALL_RCVD_LAST, &call->flags))
206                 goto protocol_error;
207
208         /* if the packet need security things doing to it, then it goes down
209          * the slow path */
210         if (call->conn->security)
211                 goto enqueue_packet;
212
213         sp->call = call;
214         rxrpc_get_call(call);
215         terminal = ((sp->hdr.flags & RXRPC_LAST_PACKET) &&
216                     !(sp->hdr.flags & RXRPC_CLIENT_INITIATED));
217         ret = rxrpc_queue_rcv_skb(call, skb, false, terminal);
218         if (ret < 0) {
219                 if (ret == -ENOMEM || ret == -ENOBUFS) {
220                         __clear_bit(ackbit, call->ackr_window);
221                         ack = RXRPC_ACK_NOSPACE;
222                         goto discard_and_ack;
223                 }
224                 goto out;
225         }
226
227         skb = NULL;
228
229         _debug("post #%u", seq);
230         ASSERTCMP(call->rx_data_post, ==, seq);
231         call->rx_data_post++;
232
233         if (sp->hdr.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_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, sp->hdr.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, sp->hdr.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, sp->hdr.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_DEAD)
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_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_RESEND_TIMER, &call->events);
303                         clear_bit(RXRPC_CALL_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 _abort_code;
322         u32 serial, hi_serial, seq, 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         /* track the latest serial number on this connection for ACK packet
340          * information */
341         serial = ntohl(sp->hdr.serial);
342         hi_serial = atomic_read(&call->conn->hi_serial);
343         while (serial > hi_serial)
344                 hi_serial = atomic_cmpxchg(&call->conn->hi_serial, hi_serial,
345                                            serial);
346
347         /* request ACK generation for any ACK or DATA packet that requests
348          * it */
349         if (sp->hdr.flags & RXRPC_REQUEST_ACK) {
350                 _proto("ACK Requested on %%%u", serial);
351                 rxrpc_propose_ACK(call, RXRPC_ACK_REQUESTED, sp->hdr.serial,
352                                   !(sp->hdr.flags & RXRPC_MORE_PACKETS));
353         }
354
355         switch (sp->hdr.type) {
356         case RXRPC_PACKET_TYPE_ABORT:
357                 _debug("abort");
358
359                 if (skb_copy_bits(skb, 0, &_abort_code,
360                                   sizeof(_abort_code)) < 0)
361                         goto protocol_error;
362
363                 abort_code = ntohl(_abort_code);
364                 _proto("Rx ABORT %%%u { %x }", serial, abort_code);
365
366                 write_lock_bh(&call->state_lock);
367                 if (call->state < RXRPC_CALL_COMPLETE) {
368                         call->state = RXRPC_CALL_REMOTELY_ABORTED;
369                         call->abort_code = abort_code;
370                         set_bit(RXRPC_CALL_RCVD_ABORT, &call->events);
371                         rxrpc_queue_call(call);
372                 }
373                 goto free_packet_unlock;
374
375         case RXRPC_PACKET_TYPE_BUSY:
376                 _proto("Rx BUSY %%%u", serial);
377
378                 if (call->conn->out_clientflag)
379                         goto protocol_error;
380
381                 write_lock_bh(&call->state_lock);
382                 switch (call->state) {
383                 case RXRPC_CALL_CLIENT_SEND_REQUEST:
384                         call->state = RXRPC_CALL_SERVER_BUSY;
385                         set_bit(RXRPC_CALL_RCVD_BUSY, &call->events);
386                         rxrpc_queue_call(call);
387                 case RXRPC_CALL_SERVER_BUSY:
388                         goto free_packet_unlock;
389                 default:
390                         goto protocol_error_locked;
391                 }
392
393         default:
394                 _proto("Rx %s %%%u", rxrpc_pkts[sp->hdr.type], serial);
395                 goto protocol_error;
396
397         case RXRPC_PACKET_TYPE_DATA:
398                 seq = ntohl(sp->hdr.seq);
399
400                 _proto("Rx DATA %%%u { #%u }", serial, seq);
401
402                 if (seq == 0)
403                         goto protocol_error;
404
405                 call->ackr_prev_seq = sp->hdr.seq;
406
407                 /* received data implicitly ACKs all of the request packets we
408                  * sent when we're acting as a client */
409                 if (call->state == RXRPC_CALL_CLIENT_AWAIT_REPLY)
410                         rxrpc_assume_implicit_ackall(call, serial);
411
412                 switch (rxrpc_fast_process_data(call, skb, seq)) {
413                 case 0:
414                         skb = NULL;
415                         goto done;
416
417                 default:
418                         BUG();
419
420                         /* data packet received beyond the last packet */
421                 case -EBADMSG:
422                         goto protocol_error;
423                 }
424
425         case RXRPC_PACKET_TYPE_ACK:
426                 /* ACK processing is done in process context */
427                 read_lock_bh(&call->state_lock);
428                 if (call->state < RXRPC_CALL_DEAD) {
429                         skb_queue_tail(&call->rx_queue, skb);
430                         rxrpc_queue_call(call);
431                         skb = NULL;
432                 }
433                 read_unlock_bh(&call->state_lock);
434                 goto free_packet;
435         }
436
437 protocol_error:
438         _debug("protocol error");
439         write_lock_bh(&call->state_lock);
440 protocol_error_locked:
441         if (call->state <= RXRPC_CALL_COMPLETE) {
442                 call->state = RXRPC_CALL_LOCALLY_ABORTED;
443                 call->abort_code = RX_PROTOCOL_ERROR;
444                 set_bit(RXRPC_CALL_ABORT, &call->events);
445                 rxrpc_queue_call(call);
446         }
447 free_packet_unlock:
448         write_unlock_bh(&call->state_lock);
449 free_packet:
450         rxrpc_free_skb(skb);
451 done:
452         _leave("");
453 }
454
455 /*
456  * split up a jumbo data packet
457  */
458 static void rxrpc_process_jumbo_packet(struct rxrpc_call *call,
459                                        struct sk_buff *jumbo)
460 {
461         struct rxrpc_jumbo_header jhdr;
462         struct rxrpc_skb_priv *sp;
463         struct sk_buff *part;
464
465         _enter(",{%u,%u}", jumbo->data_len, jumbo->len);
466
467         sp = rxrpc_skb(jumbo);
468
469         do {
470                 sp->hdr.flags &= ~RXRPC_JUMBO_PACKET;
471
472                 /* make a clone to represent the first subpacket in what's left
473                  * of the jumbo packet */
474                 part = skb_clone(jumbo, GFP_ATOMIC);
475                 if (!part) {
476                         /* simply ditch the tail in the event of ENOMEM */
477                         pskb_trim(jumbo, RXRPC_JUMBO_DATALEN);
478                         break;
479                 }
480                 rxrpc_new_skb(part);
481
482                 pskb_trim(part, RXRPC_JUMBO_DATALEN);
483
484                 if (!pskb_pull(jumbo, RXRPC_JUMBO_DATALEN))
485                         goto protocol_error;
486
487                 if (skb_copy_bits(jumbo, 0, &jhdr, sizeof(jhdr)) < 0)
488                         goto protocol_error;
489                 if (!pskb_pull(jumbo, sizeof(jhdr)))
490                         BUG();
491
492                 sp->hdr.seq     = htonl(ntohl(sp->hdr.seq) + 1);
493                 sp->hdr.serial  = htonl(ntohl(sp->hdr.serial) + 1);
494                 sp->hdr.flags   = jhdr.flags;
495                 sp->hdr._rsvd   = jhdr._rsvd;
496
497                 _proto("Rx DATA Jumbo %%%u", ntohl(sp->hdr.serial) - 1);
498
499                 rxrpc_fast_process_packet(call, part);
500                 part = NULL;
501
502         } while (sp->hdr.flags & RXRPC_JUMBO_PACKET);
503
504         rxrpc_fast_process_packet(call, jumbo);
505         _leave("");
506         return;
507
508 protocol_error:
509         _debug("protocol error");
510         rxrpc_free_skb(part);
511         rxrpc_free_skb(jumbo);
512         write_lock_bh(&call->state_lock);
513         if (call->state <= RXRPC_CALL_COMPLETE) {
514                 call->state = RXRPC_CALL_LOCALLY_ABORTED;
515                 call->abort_code = RX_PROTOCOL_ERROR;
516                 set_bit(RXRPC_CALL_ABORT, &call->events);
517                 rxrpc_queue_call(call);
518         }
519         write_unlock_bh(&call->state_lock);
520         _leave("");
521 }
522
523 /*
524  * post an incoming packet to the appropriate call/socket to deal with
525  * - must get rid of the sk_buff, either by freeing it or by queuing it
526  */
527 static void rxrpc_post_packet_to_call(struct rxrpc_connection *conn,
528                                       struct sk_buff *skb)
529 {
530         struct rxrpc_skb_priv *sp;
531         struct rxrpc_call *call;
532         struct rb_node *p;
533         __be32 call_id;
534
535         _enter("%p,%p", conn, skb);
536
537         read_lock_bh(&conn->lock);
538
539         sp = rxrpc_skb(skb);
540
541         /* look at extant calls by channel number first */
542         call = conn->channels[ntohl(sp->hdr.cid) & RXRPC_CHANNELMASK];
543         if (!call || call->call_id != sp->hdr.callNumber)
544                 goto call_not_extant;
545
546         _debug("extant call [%d]", call->state);
547         ASSERTCMP(call->conn, ==, conn);
548
549         read_lock(&call->state_lock);
550         switch (call->state) {
551         case RXRPC_CALL_LOCALLY_ABORTED:
552                 if (!test_and_set_bit(RXRPC_CALL_ABORT, &call->events))
553                         rxrpc_queue_call(call);
554         case RXRPC_CALL_REMOTELY_ABORTED:
555         case RXRPC_CALL_NETWORK_ERROR:
556         case RXRPC_CALL_DEAD:
557                 goto free_unlock;
558         default:
559                 break;
560         }
561
562         read_unlock(&call->state_lock);
563         rxrpc_get_call(call);
564         read_unlock_bh(&conn->lock);
565
566         if (sp->hdr.type == RXRPC_PACKET_TYPE_DATA &&
567             sp->hdr.flags & RXRPC_JUMBO_PACKET)
568                 rxrpc_process_jumbo_packet(call, skb);
569         else
570                 rxrpc_fast_process_packet(call, skb);
571
572         rxrpc_put_call(call);
573         goto done;
574
575 call_not_extant:
576         /* search the completed calls in case what we're dealing with is
577          * there */
578         _debug("call not extant");
579
580         call_id = sp->hdr.callNumber;
581         p = conn->calls.rb_node;
582         while (p) {
583                 call = rb_entry(p, struct rxrpc_call, conn_node);
584
585                 if (call_id < call->call_id)
586                         p = p->rb_left;
587                 else if (call_id > call->call_id)
588                         p = p->rb_right;
589                 else
590                         goto found_completed_call;
591         }
592
593 dead_call:
594         /* it's a either a really old call that we no longer remember or its a
595          * new incoming call */
596         read_unlock_bh(&conn->lock);
597
598         if (sp->hdr.flags & RXRPC_CLIENT_INITIATED &&
599             sp->hdr.seq == cpu_to_be32(1)) {
600                 _debug("incoming call");
601                 skb_queue_tail(&conn->trans->local->accept_queue, skb);
602                 rxrpc_queue_work(&conn->trans->local->acceptor);
603                 goto done;
604         }
605
606         _debug("dead call");
607         skb->priority = RX_CALL_DEAD;
608         rxrpc_reject_packet(conn->trans->local, skb);
609         goto done;
610
611         /* resend last packet of a completed call
612          * - client calls may have been aborted or ACK'd
613          * - server calls may have been aborted
614          */
615 found_completed_call:
616         _debug("completed call");
617
618         if (atomic_read(&call->usage) == 0)
619                 goto dead_call;
620
621         /* synchronise any state changes */
622         read_lock(&call->state_lock);
623         ASSERTIFCMP(call->state != RXRPC_CALL_CLIENT_FINAL_ACK,
624                     call->state, >=, RXRPC_CALL_COMPLETE);
625
626         if (call->state == RXRPC_CALL_LOCALLY_ABORTED ||
627             call->state == RXRPC_CALL_REMOTELY_ABORTED ||
628             call->state == RXRPC_CALL_DEAD) {
629                 read_unlock(&call->state_lock);
630                 goto dead_call;
631         }
632
633         if (call->conn->in_clientflag) {
634                 read_unlock(&call->state_lock);
635                 goto dead_call; /* complete server call */
636         }
637
638         _debug("final ack again");
639         rxrpc_get_call(call);
640         set_bit(RXRPC_CALL_ACK_FINAL, &call->events);
641         rxrpc_queue_call(call);
642
643 free_unlock:
644         read_unlock(&call->state_lock);
645         read_unlock_bh(&conn->lock);
646         rxrpc_free_skb(skb);
647 done:
648         _leave("");
649 }
650
651 /*
652  * post connection-level events to the connection
653  * - this includes challenges, responses and some aborts
654  */
655 static void rxrpc_post_packet_to_conn(struct rxrpc_connection *conn,
656                                       struct sk_buff *skb)
657 {
658         _enter("%p,%p", conn, skb);
659
660         atomic_inc(&conn->usage);
661         skb_queue_tail(&conn->rx_queue, skb);
662         rxrpc_queue_conn(conn);
663 }
664
665 /*
666  * handle data received on the local endpoint
667  * - may be called in interrupt context
668  */
669 void rxrpc_data_ready(struct sock *sk, int count)
670 {
671         struct rxrpc_connection *conn;
672         struct rxrpc_transport *trans;
673         struct rxrpc_skb_priv *sp;
674         struct rxrpc_local *local;
675         struct rxrpc_peer *peer;
676         struct sk_buff *skb;
677         int ret;
678
679         _enter("%p, %d", sk, count);
680
681         ASSERT(!irqs_disabled());
682
683         read_lock_bh(&rxrpc_local_lock);
684         local = sk->sk_user_data;
685         if (local && atomic_read(&local->usage) > 0)
686                 rxrpc_get_local(local);
687         else
688                 local = NULL;
689         read_unlock_bh(&rxrpc_local_lock);
690         if (!local) {
691                 _leave(" [local dead]");
692                 return;
693         }
694
695         skb = skb_recv_datagram(sk, 0, 1, &ret);
696         if (!skb) {
697                 rxrpc_put_local(local);
698                 if (ret == -EAGAIN)
699                         return;
700                 _debug("UDP socket error %d", ret);
701                 return;
702         }
703
704         rxrpc_new_skb(skb);
705
706         _net("recv skb %p", skb);
707
708         /* we'll probably need to checksum it (didn't call sock_recvmsg) */
709         if (skb_checksum_complete(skb)) {
710                 rxrpc_free_skb(skb);
711                 rxrpc_put_local(local);
712                 UDP_INC_STATS_BH(&init_net, UDP_MIB_INERRORS, 0);
713                 _leave(" [CSUM failed]");
714                 return;
715         }
716
717         UDP_INC_STATS_BH(&init_net, UDP_MIB_INDATAGRAMS, 0);
718
719         /* the socket buffer we have is owned by UDP, with UDP's data all over
720          * it, but we really want our own */
721         skb_orphan(skb);
722         sp = rxrpc_skb(skb);
723         memset(sp, 0, sizeof(*sp));
724
725         _net("Rx UDP packet from %08x:%04hu",
726              ntohl(ip_hdr(skb)->saddr), ntohs(udp_hdr(skb)->source));
727
728         /* dig out the RxRPC connection details */
729         if (skb_copy_bits(skb, sizeof(struct udphdr), &sp->hdr,
730                           sizeof(sp->hdr)) < 0)
731                 goto bad_message;
732         if (!pskb_pull(skb, sizeof(struct udphdr) + sizeof(sp->hdr)))
733                 BUG();
734
735         _net("Rx RxRPC %s ep=%x call=%x:%x",
736              sp->hdr.flags & RXRPC_CLIENT_INITIATED ? "ToServer" : "ToClient",
737              ntohl(sp->hdr.epoch),
738              ntohl(sp->hdr.cid),
739              ntohl(sp->hdr.callNumber));
740
741         if (sp->hdr.type == 0 || sp->hdr.type >= RXRPC_N_PACKET_TYPES) {
742                 _proto("Rx Bad Packet Type %u", sp->hdr.type);
743                 goto bad_message;
744         }
745
746         if (sp->hdr.type == RXRPC_PACKET_TYPE_DATA &&
747             (sp->hdr.callNumber == 0 || sp->hdr.seq == 0))
748                 goto bad_message;
749
750         peer = rxrpc_find_peer(local, ip_hdr(skb)->saddr, udp_hdr(skb)->source);
751         if (IS_ERR(peer))
752                 goto cant_route_call;
753
754         trans = rxrpc_find_transport(local, peer);
755         rxrpc_put_peer(peer);
756         if (!trans)
757                 goto cant_route_call;
758
759         conn = rxrpc_find_connection(trans, &sp->hdr);
760         rxrpc_put_transport(trans);
761         if (!conn)
762                 goto cant_route_call;
763
764         _debug("CONN %p {%d}", conn, conn->debug_id);
765
766         if (sp->hdr.callNumber == 0)
767                 rxrpc_post_packet_to_conn(conn, skb);
768         else
769                 rxrpc_post_packet_to_call(conn, skb);
770         rxrpc_put_connection(conn);
771         rxrpc_put_local(local);
772         return;
773
774 cant_route_call:
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 == cpu_to_be32(1)) {
779                         _debug("first packet");
780                         skb_queue_tail(&local->accept_queue, skb);
781                         rxrpc_queue_work(&local->acceptor);
782                         rxrpc_put_local(local);
783                         _leave(" [incoming]");
784                         return;
785                 }
786                 skb->priority = RX_INVALID_OPERATION;
787         } else {
788                 skb->priority = RX_CALL_DEAD;
789         }
790
791         _debug("reject");
792         rxrpc_reject_packet(local, skb);
793         rxrpc_put_local(local);
794         _leave(" [no call]");
795         return;
796
797 bad_message:
798         skb->priority = RX_PROTOCOL_ERROR;
799         rxrpc_reject_packet(local, skb);
800         rxrpc_put_local(local);
801         _leave(" [badmsg]");
802 }