rxrpc: Remove skb_count from struct rxrpc_call
[linux-block.git] / net / rxrpc / input.c
CommitLineData
17926a79
DH
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
9b6d5398
JP
12#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13
17926a79
DH
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>
5a0e3ad6 22#include <linux/gfp.h>
17926a79
DH
23#include <net/sock.h>
24#include <net/af_rxrpc.h>
25#include <net/ip.h>
1781f7f5 26#include <net/udp.h>
0283328e 27#include <net/net_namespace.h>
17926a79
DH
28#include "ar-internal.h"
29
17926a79
DH
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 */
38int rxrpc_queue_rcv_skb(struct rxrpc_call *call, struct sk_buff *skb,
39 bool force, bool terminal)
40{
41 struct rxrpc_skb_priv *sp;
8d94aa38 42 struct rxrpc_sock *rx;
17926a79 43 struct sock *sk;
884cf705 44 int ret;
17926a79
DH
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);
17926a79
DH
58 rxrpc_free_skb(skb);
59 return 0;
60 }
61
8d94aa38
DH
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;
651350d1 68 sk = &rx->sk;
8d94aa38
DH
69 if (sock_flag(sk, SOCK_DEAD))
70 goto out;
17926a79
DH
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 >=
95c96174 78// (unsigned int) sk->sk_rcvbuf)
17926a79
DH
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) &&
8d94aa38 89 sk->sk_state != RXRPC_CLOSE) {
17926a79
DH
90 skb->destructor = rxrpc_packet_destructor;
91 skb->dev = NULL;
92 skb->sk = sk;
93 atomic_add(skb->truesize, &sk->sk_rmem_alloc);
94
17926a79
DH
95 if (terminal) {
96 _debug("<<<< TERMINAL MESSAGE >>>>");
97 set_bit(RXRPC_CALL_TERMINAL_MSG, &call->flags);
98 }
99
651350d1 100 /* allow interception by a kernel service */
d001648e
DH
101 if (skb->mark == RXRPC_SKB_MARK_NEW_CALL &&
102 rx->notify_new_call) {
651350d1 103 spin_unlock_bh(&sk->sk_receive_queue.lock);
d001648e
DH
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);
651350d1 110 } else {
651350d1
DH
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
8d94aa38 115 sk->sk_data_ready(sk);
651350d1 116 }
17926a79
DH
117 skb = NULL;
118 } else {
119 spin_unlock_bh(&sk->sk_receive_queue.lock);
120 }
121 ret = 0;
122
123out:
372ee163 124 rxrpc_free_skb(skb);
8d94aa38 125 rcu_read_unlock();
17926a79
DH
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 */
135static 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;
50fd85a1 141 u32 serial;
563ea7d5 142 u16 skew;
50fd85a1 143 u8 flags;
17926a79
DH
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);
50fd85a1
DH
149 flags = sp->hdr.flags;
150 serial = sp->hdr.serial;
563ea7d5 151 skew = skb->priority;
17926a79
DH
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);
68c708fd 172 if (__test_and_set_bit(ackbit, call->ackr_window)) {
17926a79
DH
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);
68c708fd 181 __clear_bit(ackbit, call->ackr_window);
17926a79
DH
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 */
278ac0cd 209 if (call->security_ix)
17926a79
DH
210 goto enqueue_packet;
211
212 sp->call = call;
e34d4234 213 rxrpc_get_call_for_skb(call, skb);
50fd85a1
DH
214 terminal = ((flags & RXRPC_LAST_PACKET) &&
215 !(flags & RXRPC_CLIENT_INITIATED));
17926a79
DH
216 ret = rxrpc_queue_rcv_skb(call, skb, false, terminal);
217 if (ret < 0) {
218 if (ret == -ENOMEM || ret == -ENOBUFS) {
68c708fd 219 __clear_bit(ackbit, call->ackr_window);
17926a79
DH
220 ack = RXRPC_ACK_NOSPACE;
221 goto discard_and_ack;
222 }
223 goto out;
224 }
225
226 skb = NULL;
50fd85a1 227 sp = NULL;
17926a79
DH
228
229 _debug("post #%u", seq);
230 ASSERTCMP(call->rx_data_post, ==, seq);
231 call->rx_data_post++;
232
50fd85a1 233 if (flags & RXRPC_LAST_PACKET)
17926a79
DH
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 &&
4c198ad1 242 !test_and_set_bit(RXRPC_CALL_EV_DRAIN_RX_OOS, &call->events))
651350d1 243 rxrpc_queue_call(call);
17926a79
DH
244 read_unlock(&call->state_lock);
245 }
246
247 spin_unlock(&call->lock);
248 atomic_inc(&call->ackr_not_idle);
563ea7d5 249 rxrpc_propose_ACK(call, RXRPC_ACK_DELAY, skew, serial, false);
17926a79
DH
250 _leave(" = 0 [posted]");
251 return 0;
252
253protocol_error:
254 ret = -EBADMSG;
255out:
256 spin_unlock(&call->lock);
257 _leave(" = %d", ret);
258 return ret;
259
260discard_and_ack:
261 _debug("discard and ACK packet %p", skb);
563ea7d5 262 __rxrpc_propose_ACK(call, ack, skew, serial, true);
17926a79
DH
263discard:
264 spin_unlock(&call->lock);
265 rxrpc_free_skb(skb);
266 _leave(" = 0 [discarded]");
267 return 0;
268
269enqueue_and_ack:
563ea7d5 270 __rxrpc_propose_ACK(call, ack, skew, serial, true);
17926a79
DH
271enqueue_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);
8d94aa38 277 if (call->state < RXRPC_CALL_COMPLETE)
651350d1 278 rxrpc_queue_call(call);
17926a79
DH
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 */
288static 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);
4c198ad1 298 set_bit(RXRPC_CALL_EV_RCVD_ACKALL, &call->events);
17926a79
DH
299 write_unlock_bh(&call->state_lock);
300
301 if (try_to_del_timer_sync(&call->resend_timer) >= 0) {
4c198ad1
DH
302 clear_bit(RXRPC_CALL_EV_RESEND_TIMER, &call->events);
303 clear_bit(RXRPC_CALL_EV_RESEND, &call->events);
17926a79
DH
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 */
318void rxrpc_fast_process_packet(struct rxrpc_call *call, struct sk_buff *skb)
319{
320 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
0d12f8a4 321 __be32 wtmp;
563ea7d5 322 u32 abort_code;
17926a79
DH
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
17926a79
DH
339 /* request ACK generation for any ACK or DATA packet that requests
340 * it */
341 if (sp->hdr.flags & RXRPC_REQUEST_ACK) {
0d12f8a4 342 _proto("ACK Requested on %%%u", sp->hdr.serial);
563ea7d5
DH
343 rxrpc_propose_ACK(call, RXRPC_ACK_REQUESTED,
344 skb->priority, sp->hdr.serial, false);
17926a79
DH
345 }
346
347 switch (sp->hdr.type) {
348 case RXRPC_PACKET_TYPE_ABORT:
349 _debug("abort");
350
0d12f8a4 351 if (skb_copy_bits(skb, 0, &wtmp, sizeof(wtmp)) < 0)
17926a79
DH
352 goto protocol_error;
353
0d12f8a4
DH
354 abort_code = ntohl(wtmp);
355 _proto("Rx ABORT %%%u { %x }", sp->hdr.serial, abort_code);
17926a79 356
f5c17aae
DH
357 if (__rxrpc_set_call_completion(call,
358 RXRPC_CALL_REMOTELY_ABORTED,
359 abort_code, ECONNABORTED)) {
4c198ad1 360 set_bit(RXRPC_CALL_EV_RCVD_ABORT, &call->events);
651350d1 361 rxrpc_queue_call(call);
17926a79 362 }
f5c17aae 363 goto free_packet;
17926a79
DH
364
365 case RXRPC_PACKET_TYPE_BUSY:
0d12f8a4 366 _proto("Rx BUSY %%%u", sp->hdr.serial);
17926a79 367
6543ac52 368 if (rxrpc_is_service_call(call))
17926a79
DH
369 goto protocol_error;
370
371 write_lock_bh(&call->state_lock);
372 switch (call->state) {
373 case RXRPC_CALL_CLIENT_SEND_REQUEST:
f5c17aae
DH
374 __rxrpc_set_call_completion(call,
375 RXRPC_CALL_SERVER_BUSY,
376 0, EBUSY);
4c198ad1 377 set_bit(RXRPC_CALL_EV_RCVD_BUSY, &call->events);
651350d1 378 rxrpc_queue_call(call);
17926a79
DH
379 case RXRPC_CALL_SERVER_BUSY:
380 goto free_packet_unlock;
381 default:
382 goto protocol_error_locked;
383 }
384
385 default:
0d12f8a4 386 _proto("Rx %s %%%u", rxrpc_pkts[sp->hdr.type], sp->hdr.serial);
17926a79
DH
387 goto protocol_error;
388
389 case RXRPC_PACKET_TYPE_DATA:
0d12f8a4 390 _proto("Rx DATA %%%u { #%u }", sp->hdr.serial, sp->hdr.seq);
17926a79 391
0d12f8a4 392 if (sp->hdr.seq == 0)
17926a79
DH
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)
0d12f8a4 400 rxrpc_assume_implicit_ackall(call, sp->hdr.serial);
17926a79 401
0d12f8a4 402 switch (rxrpc_fast_process_data(call, skb, sp->hdr.seq)) {
17926a79
DH
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
10003453 415 case RXRPC_PACKET_TYPE_ACKALL:
17926a79
DH
416 case RXRPC_PACKET_TYPE_ACK:
417 /* ACK processing is done in process context */
418 read_lock_bh(&call->state_lock);
8d94aa38 419 if (call->state < RXRPC_CALL_COMPLETE) {
17926a79 420 skb_queue_tail(&call->rx_queue, skb);
651350d1 421 rxrpc_queue_call(call);
17926a79
DH
422 skb = NULL;
423 }
424 read_unlock_bh(&call->state_lock);
425 goto free_packet;
426 }
427
428protocol_error:
429 _debug("protocol error");
430 write_lock_bh(&call->state_lock);
431protocol_error_locked:
5a42976d 432 if (__rxrpc_abort_call("FPR", call, 0, RX_PROTOCOL_ERROR, EPROTO))
651350d1 433 rxrpc_queue_call(call);
17926a79
DH
434free_packet_unlock:
435 write_unlock_bh(&call->state_lock);
436free_packet:
437 rxrpc_free_skb(skb);
438done:
439 _leave("");
440}
441
442/*
443 * split up a jumbo data packet
444 */
445static 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
0d12f8a4
DH
479 sp->hdr.seq += 1;
480 sp->hdr.serial += 1;
17926a79 481 sp->hdr.flags = jhdr.flags;
ac5d2683 482 sp->hdr._rsvd = ntohs(jhdr._rsvd);
17926a79 483
0d12f8a4 484 _proto("Rx DATA Jumbo %%%u", sp->hdr.serial - 1);
17926a79
DH
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
495protocol_error:
496 _debug("protocol error");
497 rxrpc_free_skb(part);
5a42976d
DH
498 if (rxrpc_abort_call("PJP", call, sp->hdr.seq,
499 RX_PROTOCOL_ERROR, EPROTO))
651350d1 500 rxrpc_queue_call(call);
5a42976d 501 rxrpc_free_skb(jumbo);
17926a79
DH
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 */
8b7fac50
DH
509static void rxrpc_post_packet_to_call(struct rxrpc_connection *conn,
510 struct rxrpc_call *call,
17926a79
DH
511 struct sk_buff *skb)
512{
513 struct rxrpc_skb_priv *sp;
17926a79 514
7727640c 515 _enter("%p,%p", call, skb);
17926a79
DH
516
517 sp = rxrpc_skb(skb);
518
17926a79 519 _debug("extant call [%d]", call->state);
17926a79
DH
520
521 read_lock(&call->state_lock);
522 switch (call->state) {
7727640c 523 case RXRPC_CALL_COMPLETE:
f5c17aae
DH
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:
7727640c 532 goto dead_call;
f5c17aae 533 case RXRPC_CALL_SUCCEEDED:
6543ac52 534 if (rxrpc_is_service_call(call))
f5c17aae
DH
535 goto dead_call;
536 goto resend_final_ack;
537 }
538
539 case RXRPC_CALL_CLIENT_FINAL_ACK:
540 goto resend_final_ack;
541
17926a79
DH
542 default:
543 break;
544 }
545
546 read_unlock(&call->state_lock);
17926a79
DH
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
17926a79
DH
554 goto done;
555
f5c17aae
DH
556resend_final_ack:
557 _debug("final ack again");
f5c17aae
DH
558 set_bit(RXRPC_CALL_EV_ACK_FINAL, &call->events);
559 rxrpc_queue_call(call);
560 goto free_unlock;
561
17926a79 562dead_call:
b6f3a40c
TS
563 if (sp->hdr.type != RXRPC_PACKET_TYPE_ABORT) {
564 skb->priority = RX_CALL_DEAD;
8b7fac50 565 rxrpc_reject_packet(conn->params.local, skb);
7727640c 566 goto unlock;
17926a79 567 }
17926a79 568free_unlock:
17926a79 569 rxrpc_free_skb(skb);
7727640c
TS
570unlock:
571 read_unlock(&call->state_lock);
17926a79
DH
572done:
573 _leave("");
574}
575
576/*
577 * post connection-level events to the connection
18bfeba5
DH
578 * - this includes challenges, responses, some aborts and call terminal packet
579 * retransmission.
17926a79 580 */
2e7e9758 581static void rxrpc_post_packet_to_conn(struct rxrpc_connection *conn,
17926a79
DH
582 struct sk_buff *skb)
583{
584 _enter("%p,%p", conn, skb);
585
17926a79 586 skb_queue_tail(&conn->rx_queue, skb);
2e7e9758 587 rxrpc_queue_conn(conn);
17926a79
DH
588}
589
44ba0698
DH
590/*
591 * post endpoint-level events to the local endpoint
592 * - this includes debug and version messages
593 */
594static void rxrpc_post_packet_to_local(struct rxrpc_local *local,
595 struct sk_buff *skb)
596{
597 _enter("%p,%p", local, skb);
598
44ba0698 599 skb_queue_tail(&local->event_queue, skb);
5acbee46 600 rxrpc_queue_local(local);
44ba0698
DH
601}
602
0d12f8a4
DH
603/*
604 * Extract the wire header from a packet and translate the byte order.
605 */
606static noinline
607int 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 */
4d0fc73e 612 if (skb_copy_bits(skb, 0, &whdr, sizeof(whdr)) < 0)
0d12f8a4 613 return -EBADMSG;
4d0fc73e 614 if (!pskb_pull(skb, sizeof(whdr)))
0d12f8a4
DH
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
17926a79
DH
632/*
633 * handle data received on the local endpoint
634 * - may be called in interrupt context
4f95dd78
DH
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.
17926a79 639 */
676d2369 640void rxrpc_data_ready(struct sock *sk)
17926a79 641{
8496af50 642 struct rxrpc_connection *conn;
17926a79 643 struct rxrpc_skb_priv *sp;
4f95dd78 644 struct rxrpc_local *local = sk->sk_user_data;
17926a79 645 struct sk_buff *skb;
563ea7d5 646 int ret, skew;
17926a79 647
676d2369 648 _enter("%p", sk);
17926a79
DH
649
650 ASSERT(!irqs_disabled());
651
17926a79
DH
652 skb = skb_recv_datagram(sk, 0, 1, &ret);
653 if (!skb) {
17926a79
DH
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);
02c22347 667 __UDP_INC_STATS(&init_net, UDP_MIB_INERRORS, 0);
17926a79
DH
668 _leave(" [CSUM failed]");
669 return;
670 }
671
02c22347 672 __UDP_INC_STATS(&init_net, UDP_MIB_INDATAGRAMS, 0);
1781f7f5 673
0d12f8a4
DH
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 */
17926a79
DH
677 skb_orphan(skb);
678 sp = rxrpc_skb(skb);
17926a79
DH
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 */
0d12f8a4 684 if (rxrpc_extract_header(sp, skb) < 0)
17926a79 685 goto bad_message;
17926a79
DH
686
687 _net("Rx RxRPC %s ep=%x call=%x:%x",
688 sp->hdr.flags & RXRPC_CLIENT_INITIATED ? "ToServer" : "ToClient",
0d12f8a4 689 sp->hdr.epoch, sp->hdr.cid, sp->hdr.callNumber);
17926a79 690
351c1e64
DH
691 if (sp->hdr.type >= RXRPC_N_PACKET_TYPES ||
692 !((RXRPC_SUPPORTED_PACKET_TYPES >> sp->hdr.type) & 1)) {
17926a79
DH
693 _proto("Rx Bad Packet Type %u", sp->hdr.type);
694 goto bad_message;
695 }
696
44ba0698
DH
697 if (sp->hdr.type == RXRPC_PACKET_TYPE_VERSION) {
698 rxrpc_post_packet_to_local(local, skb);
699 goto out;
700 }
bc6e1ea3 701
17926a79
DH
702 if (sp->hdr.type == RXRPC_PACKET_TYPE_DATA &&
703 (sp->hdr.callNumber == 0 || sp->hdr.seq == 0))
704 goto bad_message;
705
8496af50
DH
706 rcu_read_lock();
707
8496af50 708 conn = rxrpc_find_connection_rcu(local, skb);
563ea7d5
DH
709 if (!conn) {
710 skb->priority = 0;
8496af50 711 goto cant_route_call;
563ea7d5
DH
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 }
17926a79 724
8496af50
DH
725 if (sp->hdr.callNumber == 0) {
726 /* Connection-level packet */
7727640c 727 _debug("CONN %p {%d}", conn, conn->debug_id);
2e7e9758 728 rxrpc_post_packet_to_conn(conn, skb);
18bfeba5 729 goto out_unlock;
7727640c 730 } else {
8496af50
DH
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];
18bfeba5
DH
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 */
2266ffde 744 if (rxrpc_conn_is_service(conn) &&
18bfeba5
DH
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 }
0d12f8a4 755
18bfeba5 756 call = rcu_dereference(chan->call);
8496af50 757 if (!call || atomic_read(&call->usage) == 0)
7727640c 758 goto cant_route_call;
8496af50 759
e34d4234 760 rxrpc_see_call(call);
8b7fac50 761 rxrpc_post_packet_to_call(conn, call, skb);
18bfeba5 762 goto out_unlock;
7727640c 763 }
44ba0698 764
18bfeba5
DH
765discard_unlock:
766 rxrpc_free_skb(skb);
767out_unlock:
8496af50 768 rcu_read_unlock();
44ba0698 769out:
17926a79
DH
770 return;
771
772cant_route_call:
8496af50
DH
773 rcu_read_unlock();
774
17926a79
DH
775 _debug("can't route call");
776 if (sp->hdr.flags & RXRPC_CLIENT_INITIATED &&
777 sp->hdr.type == RXRPC_PACKET_TYPE_DATA) {
0d12f8a4 778 if (sp->hdr.seq == 1) {
17926a79
DH
779 _debug("first packet");
780 skb_queue_tail(&local->accept_queue, skb);
4f95dd78 781 rxrpc_queue_work(&local->processor);
17926a79
DH
782 _leave(" [incoming]");
783 return;
784 }
785 skb->priority = RX_INVALID_OPERATION;
786 } else {
787 skb->priority = RX_CALL_DEAD;
788 }
789
b6f3a40c
TS
790 if (sp->hdr.type != RXRPC_PACKET_TYPE_ABORT) {
791 _debug("reject type %d",sp->hdr.type);
792 rxrpc_reject_packet(local, skb);
992c273a
DH
793 } else {
794 rxrpc_free_skb(skb);
b6f3a40c 795 }
17926a79
DH
796 _leave(" [no call]");
797 return;
798
799bad_message:
800 skb->priority = RX_PROTOCOL_ERROR;
801 rxrpc_reject_packet(local, skb);
17926a79
DH
802 _leave(" [badmsg]");
803}