]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - net/rxrpc/input.c
rxrpc: Trace rxrpc_call usage
[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;
e34d4234 199 rxrpc_get_call_for_skb(call, skb);
50fd85a1
DH
200 terminal = ((flags & RXRPC_LAST_PACKET) &&
201 !(flags & RXRPC_CLIENT_INITIATED));
17926a79
DH
202 ret = rxrpc_queue_rcv_skb(call, skb, false, terminal);
203 if (ret < 0) {
204 if (ret == -ENOMEM || ret == -ENOBUFS) {
68c708fd 205 __clear_bit(ackbit, call->ackr_window);
17926a79
DH
206 ack = RXRPC_ACK_NOSPACE;
207 goto discard_and_ack;
208 }
209 goto out;
210 }
211
212 skb = NULL;
50fd85a1 213 sp = NULL;
17926a79
DH
214
215 _debug("post #%u", seq);
216 ASSERTCMP(call->rx_data_post, ==, seq);
217 call->rx_data_post++;
218
50fd85a1 219 if (flags & RXRPC_LAST_PACKET)
17926a79
DH
220 set_bit(RXRPC_CALL_RCVD_LAST, &call->flags);
221
222 /* if we've reached an out of sequence packet then we need to drain
223 * that queue into the socket Rx queue now */
224 if (call->rx_data_post == call->rx_first_oos) {
225 _debug("drain rx oos now");
226 read_lock(&call->state_lock);
227 if (call->state < RXRPC_CALL_COMPLETE &&
4c198ad1 228 !test_and_set_bit(RXRPC_CALL_EV_DRAIN_RX_OOS, &call->events))
651350d1 229 rxrpc_queue_call(call);
17926a79
DH
230 read_unlock(&call->state_lock);
231 }
232
233 spin_unlock(&call->lock);
234 atomic_inc(&call->ackr_not_idle);
563ea7d5 235 rxrpc_propose_ACK(call, RXRPC_ACK_DELAY, skew, serial, false);
17926a79
DH
236 _leave(" = 0 [posted]");
237 return 0;
238
239protocol_error:
240 ret = -EBADMSG;
241out:
242 spin_unlock(&call->lock);
243 _leave(" = %d", ret);
244 return ret;
245
246discard_and_ack:
247 _debug("discard and ACK packet %p", skb);
563ea7d5 248 __rxrpc_propose_ACK(call, ack, skew, serial, true);
17926a79
DH
249discard:
250 spin_unlock(&call->lock);
251 rxrpc_free_skb(skb);
252 _leave(" = 0 [discarded]");
253 return 0;
254
255enqueue_and_ack:
563ea7d5 256 __rxrpc_propose_ACK(call, ack, skew, serial, true);
17926a79
DH
257enqueue_packet:
258 _net("defer skb %p", skb);
259 spin_unlock(&call->lock);
260 skb_queue_tail(&call->rx_queue, skb);
261 atomic_inc(&call->ackr_not_idle);
262 read_lock(&call->state_lock);
263 if (call->state < RXRPC_CALL_DEAD)
651350d1 264 rxrpc_queue_call(call);
17926a79
DH
265 read_unlock(&call->state_lock);
266 _leave(" = 0 [queued]");
267 return 0;
268}
269
270/*
271 * assume an implicit ACKALL of the transmission phase of a client socket upon
272 * reception of the first reply packet
273 */
274static void rxrpc_assume_implicit_ackall(struct rxrpc_call *call, u32 serial)
275{
276 write_lock_bh(&call->state_lock);
277
278 switch (call->state) {
279 case RXRPC_CALL_CLIENT_AWAIT_REPLY:
280 call->state = RXRPC_CALL_CLIENT_RECV_REPLY;
281 call->acks_latest = serial;
282
283 _debug("implicit ACKALL %%%u", call->acks_latest);
4c198ad1 284 set_bit(RXRPC_CALL_EV_RCVD_ACKALL, &call->events);
17926a79
DH
285 write_unlock_bh(&call->state_lock);
286
287 if (try_to_del_timer_sync(&call->resend_timer) >= 0) {
4c198ad1
DH
288 clear_bit(RXRPC_CALL_EV_RESEND_TIMER, &call->events);
289 clear_bit(RXRPC_CALL_EV_RESEND, &call->events);
17926a79
DH
290 clear_bit(RXRPC_CALL_RUN_RTIMER, &call->flags);
291 }
292 break;
293
294 default:
295 write_unlock_bh(&call->state_lock);
296 break;
297 }
298}
299
300/*
301 * post an incoming packet to the nominated call to deal with
302 * - must get rid of the sk_buff, either by freeing it or by queuing it
303 */
304void rxrpc_fast_process_packet(struct rxrpc_call *call, struct sk_buff *skb)
305{
306 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
0d12f8a4 307 __be32 wtmp;
563ea7d5 308 u32 abort_code;
17926a79
DH
309
310 _enter("%p,%p", call, skb);
311
312 ASSERT(!irqs_disabled());
313
314#if 0 // INJECT RX ERROR
315 if (sp->hdr.type == RXRPC_PACKET_TYPE_DATA) {
316 static int skip = 0;
317 if (++skip == 3) {
318 printk("DROPPED 3RD PACKET!!!!!!!!!!!!!\n");
319 skip = 0;
320 goto free_packet;
321 }
322 }
323#endif
324
17926a79
DH
325 /* request ACK generation for any ACK or DATA packet that requests
326 * it */
327 if (sp->hdr.flags & RXRPC_REQUEST_ACK) {
0d12f8a4 328 _proto("ACK Requested on %%%u", sp->hdr.serial);
563ea7d5
DH
329 rxrpc_propose_ACK(call, RXRPC_ACK_REQUESTED,
330 skb->priority, sp->hdr.serial, false);
17926a79
DH
331 }
332
333 switch (sp->hdr.type) {
334 case RXRPC_PACKET_TYPE_ABORT:
335 _debug("abort");
336
0d12f8a4 337 if (skb_copy_bits(skb, 0, &wtmp, sizeof(wtmp)) < 0)
17926a79
DH
338 goto protocol_error;
339
0d12f8a4
DH
340 abort_code = ntohl(wtmp);
341 _proto("Rx ABORT %%%u { %x }", sp->hdr.serial, abort_code);
17926a79 342
f5c17aae
DH
343 if (__rxrpc_set_call_completion(call,
344 RXRPC_CALL_REMOTELY_ABORTED,
345 abort_code, ECONNABORTED)) {
4c198ad1 346 set_bit(RXRPC_CALL_EV_RCVD_ABORT, &call->events);
651350d1 347 rxrpc_queue_call(call);
17926a79 348 }
f5c17aae 349 goto free_packet;
17926a79
DH
350
351 case RXRPC_PACKET_TYPE_BUSY:
0d12f8a4 352 _proto("Rx BUSY %%%u", sp->hdr.serial);
17926a79 353
19ffa01c 354 if (rxrpc_conn_is_service(call->conn))
17926a79
DH
355 goto protocol_error;
356
357 write_lock_bh(&call->state_lock);
358 switch (call->state) {
359 case RXRPC_CALL_CLIENT_SEND_REQUEST:
f5c17aae
DH
360 __rxrpc_set_call_completion(call,
361 RXRPC_CALL_SERVER_BUSY,
362 0, EBUSY);
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:
f5c17aae 418 if (__rxrpc_abort_call(call, RX_PROTOCOL_ERROR, EPROTO))
651350d1 419 rxrpc_queue_call(call);
17926a79
DH
420free_packet_unlock:
421 write_unlock_bh(&call->state_lock);
422free_packet:
423 rxrpc_free_skb(skb);
424done:
425 _leave("");
426}
427
428/*
429 * split up a jumbo data packet
430 */
431static void rxrpc_process_jumbo_packet(struct rxrpc_call *call,
432 struct sk_buff *jumbo)
433{
434 struct rxrpc_jumbo_header jhdr;
435 struct rxrpc_skb_priv *sp;
436 struct sk_buff *part;
437
438 _enter(",{%u,%u}", jumbo->data_len, jumbo->len);
439
440 sp = rxrpc_skb(jumbo);
441
442 do {
443 sp->hdr.flags &= ~RXRPC_JUMBO_PACKET;
444
445 /* make a clone to represent the first subpacket in what's left
446 * of the jumbo packet */
447 part = skb_clone(jumbo, GFP_ATOMIC);
448 if (!part) {
449 /* simply ditch the tail in the event of ENOMEM */
450 pskb_trim(jumbo, RXRPC_JUMBO_DATALEN);
451 break;
452 }
453 rxrpc_new_skb(part);
454
455 pskb_trim(part, RXRPC_JUMBO_DATALEN);
456
457 if (!pskb_pull(jumbo, RXRPC_JUMBO_DATALEN))
458 goto protocol_error;
459
460 if (skb_copy_bits(jumbo, 0, &jhdr, sizeof(jhdr)) < 0)
461 goto protocol_error;
462 if (!pskb_pull(jumbo, sizeof(jhdr)))
463 BUG();
464
0d12f8a4
DH
465 sp->hdr.seq += 1;
466 sp->hdr.serial += 1;
17926a79 467 sp->hdr.flags = jhdr.flags;
ac5d2683 468 sp->hdr._rsvd = ntohs(jhdr._rsvd);
17926a79 469
0d12f8a4 470 _proto("Rx DATA Jumbo %%%u", sp->hdr.serial - 1);
17926a79
DH
471
472 rxrpc_fast_process_packet(call, part);
473 part = NULL;
474
475 } while (sp->hdr.flags & RXRPC_JUMBO_PACKET);
476
477 rxrpc_fast_process_packet(call, jumbo);
478 _leave("");
479 return;
480
481protocol_error:
482 _debug("protocol error");
483 rxrpc_free_skb(part);
484 rxrpc_free_skb(jumbo);
f5c17aae 485 if (rxrpc_abort_call(call, RX_PROTOCOL_ERROR, EPROTO))
651350d1 486 rxrpc_queue_call(call);
17926a79
DH
487 _leave("");
488}
489
490/*
491 * post an incoming packet to the appropriate call/socket to deal with
492 * - must get rid of the sk_buff, either by freeing it or by queuing it
493 */
7727640c 494static void rxrpc_post_packet_to_call(struct rxrpc_call *call,
17926a79
DH
495 struct sk_buff *skb)
496{
497 struct rxrpc_skb_priv *sp;
17926a79 498
7727640c 499 _enter("%p,%p", call, skb);
17926a79
DH
500
501 sp = rxrpc_skb(skb);
502
17926a79 503 _debug("extant call [%d]", call->state);
17926a79
DH
504
505 read_lock(&call->state_lock);
506 switch (call->state) {
17926a79 507 case RXRPC_CALL_DEAD:
7727640c 508 goto dead_call;
f5c17aae 509
7727640c 510 case RXRPC_CALL_COMPLETE:
f5c17aae
DH
511 switch (call->completion) {
512 case RXRPC_CALL_LOCALLY_ABORTED:
513 if (!test_and_set_bit(RXRPC_CALL_EV_ABORT,
514 &call->events)) {
515 rxrpc_queue_call(call);
516 goto free_unlock;
517 }
518 default:
7727640c 519 goto dead_call;
f5c17aae
DH
520 case RXRPC_CALL_SUCCEEDED:
521 if (rxrpc_conn_is_service(call->conn))
522 goto dead_call;
523 goto resend_final_ack;
524 }
525
526 case RXRPC_CALL_CLIENT_FINAL_ACK:
527 goto resend_final_ack;
528
17926a79
DH
529 default:
530 break;
531 }
532
533 read_unlock(&call->state_lock);
534 rxrpc_get_call(call);
17926a79
DH
535
536 if (sp->hdr.type == RXRPC_PACKET_TYPE_DATA &&
537 sp->hdr.flags & RXRPC_JUMBO_PACKET)
538 rxrpc_process_jumbo_packet(call, skb);
539 else
540 rxrpc_fast_process_packet(call, skb);
541
542 rxrpc_put_call(call);
543 goto done;
544
f5c17aae
DH
545resend_final_ack:
546 _debug("final ack again");
547 rxrpc_get_call(call);
548 set_bit(RXRPC_CALL_EV_ACK_FINAL, &call->events);
549 rxrpc_queue_call(call);
550 goto free_unlock;
551
17926a79 552dead_call:
b6f3a40c
TS
553 if (sp->hdr.type != RXRPC_PACKET_TYPE_ABORT) {
554 skb->priority = RX_CALL_DEAD;
85f32278 555 rxrpc_reject_packet(call->conn->params.local, skb);
7727640c 556 goto unlock;
17926a79 557 }
17926a79 558free_unlock:
17926a79 559 rxrpc_free_skb(skb);
7727640c
TS
560unlock:
561 read_unlock(&call->state_lock);
17926a79
DH
562done:
563 _leave("");
564}
565
566/*
567 * post connection-level events to the connection
18bfeba5
DH
568 * - this includes challenges, responses, some aborts and call terminal packet
569 * retransmission.
17926a79 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);
18bfeba5 719 goto out_unlock;
7727640c 720 } else {
8496af50
DH
721 /* Call-bound packets are routed by connection channel. */
722 unsigned int channel = sp->hdr.cid & RXRPC_CHANNELMASK;
723 struct rxrpc_channel *chan = &conn->channels[channel];
18bfeba5
DH
724 struct rxrpc_call *call;
725
726 /* Ignore really old calls */
727 if (sp->hdr.callNumber < chan->last_call)
728 goto discard_unlock;
729
730 if (sp->hdr.callNumber == chan->last_call) {
731 /* For the previous service call, if completed
732 * successfully, we discard all further packets.
733 */
2266ffde 734 if (rxrpc_conn_is_service(conn) &&
18bfeba5
DH
735 (chan->last_type == RXRPC_PACKET_TYPE_ACK ||
736 sp->hdr.type == RXRPC_PACKET_TYPE_ABORT))
737 goto discard_unlock;
738
739 /* But otherwise we need to retransmit the final packet
740 * from data cached in the connection record.
741 */
742 rxrpc_post_packet_to_conn(conn, skb);
743 goto out_unlock;
744 }
0d12f8a4 745
18bfeba5 746 call = rcu_dereference(chan->call);
8496af50 747 if (!call || atomic_read(&call->usage) == 0)
7727640c 748 goto cant_route_call;
8496af50 749
e34d4234 750 rxrpc_see_call(call);
8496af50 751 rxrpc_post_packet_to_call(call, skb);
18bfeba5 752 goto out_unlock;
7727640c 753 }
44ba0698 754
18bfeba5
DH
755discard_unlock:
756 rxrpc_free_skb(skb);
757out_unlock:
8496af50 758 rcu_read_unlock();
44ba0698 759out:
17926a79
DH
760 return;
761
762cant_route_call:
8496af50
DH
763 rcu_read_unlock();
764
17926a79
DH
765 _debug("can't route call");
766 if (sp->hdr.flags & RXRPC_CLIENT_INITIATED &&
767 sp->hdr.type == RXRPC_PACKET_TYPE_DATA) {
0d12f8a4 768 if (sp->hdr.seq == 1) {
17926a79
DH
769 _debug("first packet");
770 skb_queue_tail(&local->accept_queue, skb);
4f95dd78 771 rxrpc_queue_work(&local->processor);
17926a79
DH
772 _leave(" [incoming]");
773 return;
774 }
775 skb->priority = RX_INVALID_OPERATION;
776 } else {
777 skb->priority = RX_CALL_DEAD;
778 }
779
b6f3a40c
TS
780 if (sp->hdr.type != RXRPC_PACKET_TYPE_ABORT) {
781 _debug("reject type %d",sp->hdr.type);
782 rxrpc_reject_packet(local, skb);
992c273a
DH
783 } else {
784 rxrpc_free_skb(skb);
b6f3a40c 785 }
17926a79
DH
786 _leave(" [no call]");
787 return;
788
789bad_message:
790 skb->priority = RX_PROTOCOL_ERROR;
791 rxrpc_reject_packet(local, skb);
17926a79
DH
792 _leave(" [badmsg]");
793}