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