1 /* RxRPC packet reception
3 * Copyright (C) 2007, 2016 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
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.
12 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
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>
20 #include <linux/in6.h>
21 #include <linux/icmp.h>
22 #include <linux/gfp.h>
24 #include <net/af_rxrpc.h>
27 #include <net/net_namespace.h>
28 #include "ar-internal.h"
30 static void rxrpc_proto_abort(const char *why,
31 struct rxrpc_call *call, rxrpc_seq_t seq)
33 if (rxrpc_abort_call(why, call, seq, RX_PROTOCOL_ERROR, EBADMSG)) {
34 set_bit(RXRPC_CALL_EV_ABORT, &call->events);
35 rxrpc_queue_call(call);
40 * Ping the other end to fill our RTT cache and to retrieve the rwind
43 static void rxrpc_send_ping(struct rxrpc_call *call, struct sk_buff *skb,
46 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
47 ktime_t now = skb->tstamp;
49 if (call->peer->rtt_usage < 3 ||
50 ktime_before(ktime_add_ms(call->peer->rtt_last_req, 1000), now))
51 rxrpc_propose_ACK(call, RXRPC_ACK_PING, skew, sp->hdr.serial,
53 rxrpc_propose_ack_ping_for_params);
57 * Apply a hard ACK by advancing the Tx window.
59 static void rxrpc_rotate_tx_window(struct rxrpc_call *call, rxrpc_seq_t to)
61 struct sk_buff *skb, *list = NULL;
65 spin_lock(&call->lock);
67 while (before(call->tx_hard_ack, to)) {
69 ix = call->tx_hard_ack & RXRPC_RXTX_BUFF_MASK;
70 skb = call->rxtx_buffer[ix];
71 annotation = call->rxtx_annotations[ix];
72 rxrpc_see_skb(skb, rxrpc_skb_tx_rotated);
73 call->rxtx_buffer[ix] = NULL;
74 call->rxtx_annotations[ix] = 0;
78 if (annotation & RXRPC_TX_ANNO_LAST)
79 set_bit(RXRPC_CALL_TX_LAST, &call->flags);
82 spin_unlock(&call->lock);
84 trace_rxrpc_transmit(call, (test_bit(RXRPC_CALL_TX_LAST, &call->flags) ?
85 rxrpc_transmit_rotate_last :
86 rxrpc_transmit_rotate));
87 wake_up(&call->waitq);
93 rxrpc_free_skb(skb, rxrpc_skb_tx_freed);
98 * End the transmission phase of a call.
100 * This occurs when we get an ACKALL packet, the first DATA packet of a reply,
101 * or a final ACK packet.
103 static bool rxrpc_end_tx_phase(struct rxrpc_call *call, bool reply_begun,
104 const char *abort_why)
107 ASSERT(test_bit(RXRPC_CALL_TX_LAST, &call->flags));
109 write_lock(&call->state_lock);
111 switch (call->state) {
112 case RXRPC_CALL_CLIENT_SEND_REQUEST:
113 case RXRPC_CALL_CLIENT_AWAIT_REPLY:
115 call->state = RXRPC_CALL_CLIENT_RECV_REPLY;
117 call->state = RXRPC_CALL_CLIENT_AWAIT_REPLY;
120 case RXRPC_CALL_SERVER_AWAIT_ACK:
121 __rxrpc_call_completed(call);
122 rxrpc_notify_socket(call);
129 write_unlock(&call->state_lock);
130 if (call->state == RXRPC_CALL_CLIENT_AWAIT_REPLY) {
131 trace_rxrpc_transmit(call, rxrpc_transmit_await_reply);
133 trace_rxrpc_transmit(call, rxrpc_transmit_end);
139 write_unlock(&call->state_lock);
140 kdebug("end_tx %s", rxrpc_call_states[call->state]);
141 rxrpc_proto_abort(abort_why, call, call->tx_top);
146 * Begin the reply reception phase of a call.
148 static bool rxrpc_receiving_reply(struct rxrpc_call *call)
150 rxrpc_seq_t top = READ_ONCE(call->tx_top);
152 if (!test_bit(RXRPC_CALL_TX_LAST, &call->flags))
153 rxrpc_rotate_tx_window(call, top);
154 if (!test_bit(RXRPC_CALL_TX_LAST, &call->flags)) {
155 rxrpc_proto_abort("TXL", call, top);
158 if (!rxrpc_end_tx_phase(call, true, "ETD"))
160 call->tx_phase = false;
165 * Scan a jumbo packet to validate its structure and to work out how many
166 * subpackets it contains.
168 * A jumbo packet is a collection of consecutive packets glued together with
169 * little headers between that indicate how to change the initial header for
172 * RXRPC_JUMBO_PACKET must be set on all but the last subpacket - and all but
173 * the last are RXRPC_JUMBO_DATALEN in size. The last subpacket may be of any
176 static bool rxrpc_validate_jumbo(struct sk_buff *skb)
178 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
179 unsigned int offset = sp->offset;
180 unsigned int len = skb->len;
182 u8 flags = sp->hdr.flags;
186 if (len - offset < RXRPC_JUMBO_SUBPKTLEN)
188 if (flags & RXRPC_LAST_PACKET)
190 offset += RXRPC_JUMBO_DATALEN;
191 if (skb_copy_bits(skb, offset, &flags, 1) < 0)
193 offset += sizeof(struct rxrpc_jumbo_header);
194 } while (flags & RXRPC_JUMBO_PACKET);
196 sp->nr_jumbo = nr_jumbo;
204 * Handle reception of a duplicate packet.
206 * We have to take care to avoid an attack here whereby we're given a series of
207 * jumbograms, each with a sequence number one before the preceding one and
208 * filled up to maximum UDP size. If they never send us the first packet in
209 * the sequence, they can cause us to have to hold on to around 2MiB of kernel
210 * space until the call times out.
212 * We limit the space usage by only accepting three duplicate jumbo packets per
213 * call. After that, we tell the other side we're no longer accepting jumbos
214 * (that information is encoded in the ACK packet).
216 static void rxrpc_input_dup_data(struct rxrpc_call *call, rxrpc_seq_t seq,
217 u8 annotation, bool *_jumbo_bad)
219 /* Discard normal packets that are duplicates. */
223 /* Skip jumbo subpackets that are duplicates. When we've had three or
224 * more partially duplicate jumbo packets, we refuse to take any more
225 * jumbos for this call.
228 call->nr_jumbo_bad++;
234 * Process a DATA packet, adding the packet to the Rx ring.
236 static void rxrpc_input_data(struct rxrpc_call *call, struct sk_buff *skb,
239 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
240 unsigned int offset = sp->offset;
242 rxrpc_serial_t serial = sp->hdr.serial, ack_serial = 0;
243 rxrpc_seq_t seq = sp->hdr.seq, hard_ack;
244 bool immediate_ack = false, jumbo_bad = false, queued;
246 u8 ack = 0, flags, annotation = 0;
248 _enter("{%u,%u},{%u,%u}",
249 call->rx_hard_ack, call->rx_top, skb->len, seq);
251 _proto("Rx DATA %%%u { #%u f=%02x }",
252 sp->hdr.serial, seq, sp->hdr.flags);
254 if (call->state >= RXRPC_CALL_COMPLETE)
257 /* Received data implicitly ACKs all of the request packets we sent
258 * when we're acting as a client.
260 if ((call->state == RXRPC_CALL_CLIENT_SEND_REQUEST ||
261 call->state == RXRPC_CALL_CLIENT_AWAIT_REPLY) &&
262 !rxrpc_receiving_reply(call))
265 call->ackr_prev_seq = seq;
267 hard_ack = READ_ONCE(call->rx_hard_ack);
268 if (after(seq, hard_ack + call->rx_winsize)) {
269 ack = RXRPC_ACK_EXCEEDS_WINDOW;
274 flags = sp->hdr.flags;
275 if (flags & RXRPC_JUMBO_PACKET) {
276 if (call->nr_jumbo_bad > 3) {
277 ack = RXRPC_ACK_NOSPACE;
286 ix = seq & RXRPC_RXTX_BUFF_MASK;
288 if (flags & RXRPC_JUMBO_PACKET)
289 len = RXRPC_JUMBO_DATALEN;
291 if (flags & RXRPC_LAST_PACKET) {
292 if (test_bit(RXRPC_CALL_RX_LAST, &call->flags) &&
294 return rxrpc_proto_abort("LSN", call, seq);
296 if (test_bit(RXRPC_CALL_RX_LAST, &call->flags) &&
297 after_eq(seq, call->rx_top))
298 return rxrpc_proto_abort("LSA", call, seq);
301 if (before_eq(seq, hard_ack)) {
302 ack = RXRPC_ACK_DUPLICATE;
307 if (flags & RXRPC_REQUEST_ACK && !ack) {
308 ack = RXRPC_ACK_REQUESTED;
312 if (call->rxtx_buffer[ix]) {
313 rxrpc_input_dup_data(call, seq, annotation, &jumbo_bad);
314 if (ack != RXRPC_ACK_DUPLICATE) {
315 ack = RXRPC_ACK_DUPLICATE;
318 immediate_ack = true;
322 /* Queue the packet. We use a couple of memory barriers here as need
323 * to make sure that rx_top is perceived to be set after the buffer
324 * pointer and that the buffer pointer is set after the annotation and
327 * Barriers against rxrpc_recvmsg_data() and rxrpc_rotate_rx_window()
328 * and also rxrpc_fill_out_ack().
330 rxrpc_get_skb(skb, rxrpc_skb_rx_got);
331 call->rxtx_annotations[ix] = annotation;
333 call->rxtx_buffer[ix] = skb;
334 if (after(seq, call->rx_top)) {
335 smp_store_release(&call->rx_top, seq);
336 } else if (before(seq, call->rx_top)) {
337 /* Send an immediate ACK if we fill in a hole */
339 ack = RXRPC_ACK_DELAY;
342 immediate_ack = true;
344 if (flags & RXRPC_LAST_PACKET) {
345 set_bit(RXRPC_CALL_RX_LAST, &call->flags);
346 trace_rxrpc_receive(call, rxrpc_receive_queue_last, serial, seq);
348 trace_rxrpc_receive(call, rxrpc_receive_queue, serial, seq);
352 if (after_eq(seq, call->rx_expect_next)) {
353 if (after(seq, call->rx_expect_next)) {
354 _net("OOS %u > %u", seq, call->rx_expect_next);
355 ack = RXRPC_ACK_OUT_OF_SEQUENCE;
358 call->rx_expect_next = seq + 1;
363 if (flags & RXRPC_JUMBO_PACKET) {
364 if (skb_copy_bits(skb, offset, &flags, 1) < 0)
365 return rxrpc_proto_abort("XJF", call, seq);
366 offset += sizeof(struct rxrpc_jumbo_header);
370 if (flags & RXRPC_JUMBO_PACKET)
371 annotation |= RXRPC_RX_ANNO_JLAST;
372 if (after(seq, hard_ack + call->rx_winsize)) {
373 ack = RXRPC_ACK_EXCEEDS_WINDOW;
376 call->nr_jumbo_bad++;
382 _proto("Rx DATA Jumbo %%%u", serial);
386 if (queued && flags & RXRPC_LAST_PACKET && !ack) {
387 ack = RXRPC_ACK_DELAY;
393 rxrpc_propose_ACK(call, ack, skew, ack_serial,
395 rxrpc_propose_ack_input_data);
397 if (sp->hdr.seq == READ_ONCE(call->rx_hard_ack) + 1)
398 rxrpc_notify_socket(call);
403 * Process a requested ACK.
405 static void rxrpc_input_requested_ack(struct rxrpc_call *call,
407 rxrpc_serial_t orig_serial,
408 rxrpc_serial_t ack_serial)
410 struct rxrpc_skb_priv *sp;
415 for (ix = 0; ix < RXRPC_RXTX_BUFF_SIZE; ix++) {
416 skb = call->rxtx_buffer[ix];
421 if (sp->hdr.serial != orig_serial)
424 sent_at = skb->tstamp;
430 rxrpc_peer_add_rtt(call, rxrpc_rtt_rx_requested_ack,
431 orig_serial, ack_serial, sent_at, resp_time);
435 * Process a ping response.
437 static void rxrpc_input_ping_response(struct rxrpc_call *call,
439 rxrpc_serial_t orig_serial,
440 rxrpc_serial_t ack_serial)
442 rxrpc_serial_t ping_serial;
445 ping_time = call->ackr_ping_time;
447 ping_serial = call->ackr_ping;
449 if (!test_bit(RXRPC_CALL_PINGING, &call->flags) ||
450 before(orig_serial, ping_serial))
452 clear_bit(RXRPC_CALL_PINGING, &call->flags);
453 if (after(orig_serial, ping_serial))
456 rxrpc_peer_add_rtt(call, rxrpc_rtt_rx_ping_response,
457 orig_serial, ack_serial, ping_time, resp_time);
461 * Process the extra information that may be appended to an ACK packet
463 static void rxrpc_input_ackinfo(struct rxrpc_call *call, struct sk_buff *skb,
464 struct rxrpc_ackinfo *ackinfo)
466 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
467 struct rxrpc_peer *peer;
469 u32 rwind = ntohl(ackinfo->rwind);
471 _proto("Rx ACK %%%u Info { rx=%u max=%u rwin=%u jm=%u }",
473 ntohl(ackinfo->rxMTU), ntohl(ackinfo->maxMTU),
474 rwind, ntohl(ackinfo->jumbo_max));
476 if (rwind > RXRPC_RXTX_BUFF_SIZE - 1)
477 rwind = RXRPC_RXTX_BUFF_SIZE - 1;
478 call->tx_winsize = rwind;
480 mtu = min(ntohl(ackinfo->rxMTU), ntohl(ackinfo->maxMTU));
483 if (mtu < peer->maxdata) {
484 spin_lock_bh(&peer->lock);
486 peer->mtu = mtu + peer->hdrsize;
487 spin_unlock_bh(&peer->lock);
488 _net("Net MTU %u (maxdata %u)", peer->mtu, peer->maxdata);
493 * Process individual soft ACKs.
495 * Each ACK in the array corresponds to one packet and can be either an ACK or
496 * a NAK. If we get find an explicitly NAK'd packet we resend immediately;
497 * packets that lie beyond the end of the ACK list are scheduled for resend by
498 * the timer on the basis that the peer might just not have processed them at
499 * the time the ACK was sent.
501 static void rxrpc_input_soft_acks(struct rxrpc_call *call, u8 *acks,
502 rxrpc_seq_t seq, int nr_acks)
506 u8 annotation, anno_type;
508 for (; nr_acks > 0; nr_acks--, seq++) {
509 ix = seq & RXRPC_RXTX_BUFF_MASK;
510 annotation = call->rxtx_annotations[ix];
511 anno_type = annotation & RXRPC_TX_ANNO_MASK;
512 annotation &= ~RXRPC_TX_ANNO_MASK;
514 case RXRPC_ACK_TYPE_ACK:
515 if (anno_type == RXRPC_TX_ANNO_ACK)
517 call->rxtx_annotations[ix] =
518 RXRPC_TX_ANNO_ACK | annotation;
520 case RXRPC_ACK_TYPE_NACK:
521 if (anno_type == RXRPC_TX_ANNO_NAK)
523 if (anno_type == RXRPC_TX_ANNO_RETRANS)
525 call->rxtx_annotations[ix] =
526 RXRPC_TX_ANNO_NAK | annotation;
530 return rxrpc_proto_abort("SFT", call, 0);
535 !test_and_set_bit(RXRPC_CALL_EV_RESEND, &call->events))
536 rxrpc_queue_call(call);
540 * Process an ACK packet.
542 * ack.firstPacket is the sequence number of the first soft-ACK'd/NAK'd packet
543 * in the ACK array. Anything before that is hard-ACK'd and may be discarded.
545 * A hard-ACK means that a packet has been processed and may be discarded; a
546 * soft-ACK means that the packet may be discarded and retransmission
547 * requested. A phase is complete when all packets are hard-ACK'd.
549 static void rxrpc_input_ack(struct rxrpc_call *call, struct sk_buff *skb,
553 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
555 struct rxrpc_ackpacket ack;
556 struct rxrpc_ackinfo info;
557 u8 acks[RXRPC_MAXACKS];
559 rxrpc_serial_t acked_serial;
560 rxrpc_seq_t first_soft_ack, hard_ack;
565 if (skb_copy_bits(skb, sp->offset, &buf.ack, sizeof(buf.ack)) < 0) {
566 _debug("extraction failure");
567 return rxrpc_proto_abort("XAK", call, 0);
569 sp->offset += sizeof(buf.ack);
571 acked_serial = ntohl(buf.ack.serial);
572 first_soft_ack = ntohl(buf.ack.firstPacket);
573 hard_ack = first_soft_ack - 1;
574 nr_acks = buf.ack.nAcks;
575 ack_reason = (buf.ack.reason < RXRPC_ACK__INVALID ?
576 buf.ack.reason : RXRPC_ACK__INVALID);
578 trace_rxrpc_rx_ack(call, first_soft_ack, ack_reason, nr_acks);
580 _proto("Rx ACK %%%u { m=%hu f=#%u p=#%u s=%%%u r=%s n=%u }",
582 ntohs(buf.ack.maxSkew),
584 ntohl(buf.ack.previousPacket),
586 rxrpc_ack_names[ack_reason],
589 if (buf.ack.reason == RXRPC_ACK_PING_RESPONSE)
590 rxrpc_input_ping_response(call, skb->tstamp, acked_serial,
592 if (buf.ack.reason == RXRPC_ACK_REQUESTED)
593 rxrpc_input_requested_ack(call, skb->tstamp, acked_serial,
596 if (buf.ack.reason == RXRPC_ACK_PING) {
597 _proto("Rx ACK %%%u PING Request", sp->hdr.serial);
598 rxrpc_propose_ACK(call, RXRPC_ACK_PING_RESPONSE,
599 skew, sp->hdr.serial, true, true,
600 rxrpc_propose_ack_respond_to_ping);
601 } else if (sp->hdr.flags & RXRPC_REQUEST_ACK) {
602 rxrpc_propose_ACK(call, RXRPC_ACK_REQUESTED,
603 skew, sp->hdr.serial, true, true,
604 rxrpc_propose_ack_respond_to_ack);
607 offset = sp->offset + nr_acks + 3;
608 if (skb->len >= offset + sizeof(buf.info)) {
609 if (skb_copy_bits(skb, offset, &buf.info, sizeof(buf.info)) < 0)
610 return rxrpc_proto_abort("XAI", call, 0);
611 rxrpc_input_ackinfo(call, skb, &buf.info);
614 if (first_soft_ack == 0)
615 return rxrpc_proto_abort("AK0", call, 0);
617 /* Ignore ACKs unless we are or have just been transmitting. */
618 switch (call->state) {
619 case RXRPC_CALL_CLIENT_SEND_REQUEST:
620 case RXRPC_CALL_CLIENT_AWAIT_REPLY:
621 case RXRPC_CALL_SERVER_SEND_REPLY:
622 case RXRPC_CALL_SERVER_AWAIT_ACK:
628 /* Discard any out-of-order or duplicate ACKs. */
629 if (before_eq(sp->hdr.serial, call->acks_latest)) {
630 _debug("discard ACK %d <= %d",
631 sp->hdr.serial, call->acks_latest);
634 call->acks_latest = sp->hdr.serial;
636 if (before(hard_ack, call->tx_hard_ack) ||
637 after(hard_ack, call->tx_top))
638 return rxrpc_proto_abort("AKW", call, 0);
639 if (nr_acks > call->tx_top - hard_ack)
640 return rxrpc_proto_abort("AKN", call, 0);
642 if (after(hard_ack, call->tx_hard_ack))
643 rxrpc_rotate_tx_window(call, hard_ack);
646 if (skb_copy_bits(skb, sp->offset, buf.acks, nr_acks) < 0)
647 return rxrpc_proto_abort("XSA", call, 0);
648 rxrpc_input_soft_acks(call, buf.acks, first_soft_ack, nr_acks);
651 if (test_bit(RXRPC_CALL_TX_LAST, &call->flags)) {
652 rxrpc_end_tx_phase(call, false, "ETA");
659 * Process an ACKALL packet.
661 static void rxrpc_input_ackall(struct rxrpc_call *call, struct sk_buff *skb)
663 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
665 _proto("Rx ACKALL %%%u", sp->hdr.serial);
667 rxrpc_rotate_tx_window(call, call->tx_top);
668 if (test_bit(RXRPC_CALL_TX_LAST, &call->flags))
669 rxrpc_end_tx_phase(call, false, "ETL");
673 * Process an ABORT packet.
675 static void rxrpc_input_abort(struct rxrpc_call *call, struct sk_buff *skb)
677 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
679 u32 abort_code = RX_CALL_DEAD;
684 skb_copy_bits(skb, sp->offset, &wtmp, sizeof(wtmp)) >= 0)
685 abort_code = ntohl(wtmp);
687 _proto("Rx ABORT %%%u { %x }", sp->hdr.serial, abort_code);
689 if (rxrpc_set_call_completion(call, RXRPC_CALL_REMOTELY_ABORTED,
690 abort_code, ECONNABORTED))
691 rxrpc_notify_socket(call);
695 * Process an incoming call packet.
697 static void rxrpc_input_call_packet(struct rxrpc_call *call,
698 struct sk_buff *skb, u16 skew)
700 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
702 _enter("%p,%p", call, skb);
704 switch (sp->hdr.type) {
705 case RXRPC_PACKET_TYPE_DATA:
706 rxrpc_input_data(call, skb, skew);
709 case RXRPC_PACKET_TYPE_ACK:
710 rxrpc_input_ack(call, skb, skew);
713 case RXRPC_PACKET_TYPE_BUSY:
714 _proto("Rx BUSY %%%u", sp->hdr.serial);
716 /* Just ignore BUSY packets from the server; the retry and
717 * lifespan timers will take care of business. BUSY packets
718 * from the client don't make sense.
722 case RXRPC_PACKET_TYPE_ABORT:
723 rxrpc_input_abort(call, skb);
726 case RXRPC_PACKET_TYPE_ACKALL:
727 rxrpc_input_ackall(call, skb);
731 _proto("Rx %s %%%u", rxrpc_pkts[sp->hdr.type], sp->hdr.serial);
739 * post connection-level events to the connection
740 * - this includes challenges, responses, some aborts and call terminal packet
743 static void rxrpc_post_packet_to_conn(struct rxrpc_connection *conn,
746 _enter("%p,%p", conn, skb);
748 skb_queue_tail(&conn->rx_queue, skb);
749 rxrpc_queue_conn(conn);
753 * post endpoint-level events to the local endpoint
754 * - this includes debug and version messages
756 static void rxrpc_post_packet_to_local(struct rxrpc_local *local,
759 _enter("%p,%p", local, skb);
761 skb_queue_tail(&local->event_queue, skb);
762 rxrpc_queue_local(local);
766 * put a packet up for transport-level abort
768 static void rxrpc_reject_packet(struct rxrpc_local *local, struct sk_buff *skb)
770 CHECK_SLAB_OKAY(&local->usage);
772 skb_queue_tail(&local->reject_queue, skb);
773 rxrpc_queue_local(local);
777 * Extract the wire header from a packet and translate the byte order.
780 int rxrpc_extract_header(struct rxrpc_skb_priv *sp, struct sk_buff *skb)
782 struct rxrpc_wire_header whdr;
784 /* dig out the RxRPC connection details */
785 if (skb_copy_bits(skb, 0, &whdr, sizeof(whdr)) < 0)
788 memset(sp, 0, sizeof(*sp));
789 sp->hdr.epoch = ntohl(whdr.epoch);
790 sp->hdr.cid = ntohl(whdr.cid);
791 sp->hdr.callNumber = ntohl(whdr.callNumber);
792 sp->hdr.seq = ntohl(whdr.seq);
793 sp->hdr.serial = ntohl(whdr.serial);
794 sp->hdr.flags = whdr.flags;
795 sp->hdr.type = whdr.type;
796 sp->hdr.userStatus = whdr.userStatus;
797 sp->hdr.securityIndex = whdr.securityIndex;
798 sp->hdr._rsvd = ntohs(whdr._rsvd);
799 sp->hdr.serviceId = ntohs(whdr.serviceId);
800 sp->offset = sizeof(whdr);
805 * handle data received on the local endpoint
806 * - may be called in interrupt context
808 * The socket is locked by the caller and this prevents the socket from being
809 * shut down and the local endpoint from going away, thus sk_user_data will not
810 * be cleared until this function returns.
812 void rxrpc_data_ready(struct sock *udp_sk)
814 struct rxrpc_connection *conn;
815 struct rxrpc_channel *chan;
816 struct rxrpc_call *call;
817 struct rxrpc_skb_priv *sp;
818 struct rxrpc_local *local = udp_sk->sk_user_data;
820 unsigned int channel;
823 _enter("%p", udp_sk);
825 ASSERT(!irqs_disabled());
827 skb = skb_recv_datagram(udp_sk, 0, 1, &ret);
831 _debug("UDP socket error %d", ret);
835 rxrpc_new_skb(skb, rxrpc_skb_rx_received);
837 _net("recv skb %p", skb);
839 /* we'll probably need to checksum it (didn't call sock_recvmsg) */
840 if (skb_checksum_complete(skb)) {
841 rxrpc_free_skb(skb, rxrpc_skb_rx_freed);
842 __UDP_INC_STATS(&init_net, UDP_MIB_INERRORS, 0);
843 _leave(" [CSUM failed]");
847 __UDP_INC_STATS(&init_net, UDP_MIB_INDATAGRAMS, 0);
849 /* The socket buffer we have is owned by UDP, with UDP's data all over
850 * it, but we really want our own data there.
855 /* dig out the RxRPC connection details */
856 if (rxrpc_extract_header(sp, skb) < 0)
859 if (IS_ENABLED(CONFIG_AF_RXRPC_INJECT_LOSS)) {
861 if ((lose++ & 7) == 7) {
862 trace_rxrpc_rx_lose(sp);
863 rxrpc_lose_skb(skb, rxrpc_skb_rx_lost);
868 trace_rxrpc_rx_packet(sp);
870 _net("Rx RxRPC %s ep=%x call=%x:%x",
871 sp->hdr.flags & RXRPC_CLIENT_INITIATED ? "ToServer" : "ToClient",
872 sp->hdr.epoch, sp->hdr.cid, sp->hdr.callNumber);
874 if (sp->hdr.type >= RXRPC_N_PACKET_TYPES ||
875 !((RXRPC_SUPPORTED_PACKET_TYPES >> sp->hdr.type) & 1)) {
876 _proto("Rx Bad Packet Type %u", sp->hdr.type);
880 switch (sp->hdr.type) {
881 case RXRPC_PACKET_TYPE_VERSION:
882 rxrpc_post_packet_to_local(local, skb);
885 case RXRPC_PACKET_TYPE_BUSY:
886 if (sp->hdr.flags & RXRPC_CLIENT_INITIATED)
889 case RXRPC_PACKET_TYPE_DATA:
890 if (sp->hdr.callNumber == 0)
892 if (sp->hdr.flags & RXRPC_JUMBO_PACKET &&
893 !rxrpc_validate_jumbo(skb))
900 conn = rxrpc_find_connection_rcu(local, skb);
902 if (sp->hdr.securityIndex != conn->security_ix)
905 if (sp->hdr.callNumber == 0) {
906 /* Connection-level packet */
907 _debug("CONN %p {%d}", conn, conn->debug_id);
908 rxrpc_post_packet_to_conn(conn, skb);
912 /* Note the serial number skew here */
913 skew = (int)sp->hdr.serial - (int)conn->hi_serial;
916 conn->hi_serial = sp->hdr.serial;
919 skew = min(skew, 65535);
922 /* Call-bound packets are routed by connection channel. */
923 channel = sp->hdr.cid & RXRPC_CHANNELMASK;
924 chan = &conn->channels[channel];
926 /* Ignore really old calls */
927 if (sp->hdr.callNumber < chan->last_call)
930 if (sp->hdr.callNumber == chan->last_call) {
931 /* For the previous service call, if completed successfully, we
932 * discard all further packets.
934 if (rxrpc_conn_is_service(conn) &&
935 (chan->last_type == RXRPC_PACKET_TYPE_ACK ||
936 sp->hdr.type == RXRPC_PACKET_TYPE_ABORT))
939 /* But otherwise we need to retransmit the final packet from
940 * data cached in the connection record.
942 rxrpc_post_packet_to_conn(conn, skb);
946 call = rcu_dereference(chan->call);
952 if (!call || atomic_read(&call->usage) == 0) {
953 if (!(sp->hdr.type & RXRPC_CLIENT_INITIATED) ||
954 sp->hdr.callNumber == 0 ||
955 sp->hdr.type != RXRPC_PACKET_TYPE_DATA)
956 goto bad_message_unlock;
957 if (sp->hdr.seq != 1)
959 call = rxrpc_new_incoming_call(local, conn, skb);
964 rxrpc_send_ping(call, skb, skew);
967 rxrpc_input_call_packet(call, skb, skew);
973 rxrpc_free_skb(skb, rxrpc_skb_rx_freed);
975 trace_rxrpc_rx_done(0, 0);
984 trace_rxrpc_abort("SEC", sp->hdr.cid, sp->hdr.callNumber, sp->hdr.seq,
985 RXKADINCONSISTENCY, EBADMSG);
986 skb->priority = RXKADINCONSISTENCY;
992 trace_rxrpc_abort("BAD", sp->hdr.cid, sp->hdr.callNumber, sp->hdr.seq,
993 RX_PROTOCOL_ERROR, EBADMSG);
994 skb->priority = RX_PROTOCOL_ERROR;
996 skb->mark = RXRPC_SKB_MARK_LOCAL_ABORT;
998 trace_rxrpc_rx_done(skb->mark, skb->priority);
999 rxrpc_reject_packet(local, skb);
1000 _leave(" [badmsg]");