1 /* RxRPC packet reception
3 * Copyright (C) 2007 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 #include <linux/module.h>
13 #include <linux/net.h>
14 #include <linux/skbuff.h>
15 #include <linux/errqueue.h>
16 #include <linux/udp.h>
18 #include <linux/in6.h>
19 #include <linux/icmp.h>
21 #include <net/af_rxrpc.h>
24 #include <net/net_namespace.h>
25 #include "ar-internal.h"
27 unsigned long rxrpc_ack_timeout = 1;
29 const char *rxrpc_pkts[] = {
31 "DATA", "ACK", "BUSY", "ABORT", "ACKALL", "CHALL", "RESP", "DEBUG",
32 "?09", "?10", "?11", "?12", "?13", "?14", "?15"
36 * queue a packet for recvmsg to pass to userspace
37 * - the caller must hold a lock on call->lock
38 * - must not be called with interrupts disabled (sk_filter() disables BH's)
39 * - eats the packet whether successful or not
40 * - there must be just one reference to the packet, which the caller passes to
43 int rxrpc_queue_rcv_skb(struct rxrpc_call *call, struct sk_buff *skb,
44 bool force, bool terminal)
46 struct rxrpc_skb_priv *sp;
47 struct rxrpc_sock *rx = call->socket;
51 _enter(",,%d,%d", force, terminal);
53 ASSERT(!irqs_disabled());
56 ASSERTCMP(sp->call, ==, call);
58 /* if we've already posted the terminal message for a call, then we
59 * don't post any more */
60 if (test_bit(RXRPC_CALL_TERMINAL_MSG, &call->flags)) {
61 _debug("already terminated");
62 ASSERTCMP(call->state, >=, RXRPC_CALL_COMPLETE);
63 skb->destructor = NULL;
73 /* cast skb->rcvbuf to unsigned... It's pointless, but
74 * reduces number of warnings when compiling with -W
77 // if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >=
78 // (unsigned) sk->sk_rcvbuf)
81 ret = sk_filter(sk, skb);
86 spin_lock_bh(&sk->sk_receive_queue.lock);
87 if (!test_bit(RXRPC_CALL_TERMINAL_MSG, &call->flags) &&
88 !test_bit(RXRPC_CALL_RELEASED, &call->flags) &&
89 call->socket->sk.sk_state != RXRPC_CLOSE) {
90 skb->destructor = rxrpc_packet_destructor;
93 atomic_add(skb->truesize, &sk->sk_rmem_alloc);
96 _debug("<<<< TERMINAL MESSAGE >>>>");
97 set_bit(RXRPC_CALL_TERMINAL_MSG, &call->flags);
100 /* allow interception by a kernel service */
101 if (rx->interceptor) {
102 rx->interceptor(sk, call->user_call_ID, skb);
103 spin_unlock_bh(&sk->sk_receive_queue.lock);
106 /* Cache the SKB length before we tack it onto the
107 * receive queue. Once it is added it no longer
108 * belongs to us and may be freed by other threads of
109 * control pulling packets from the queue */
112 _net("post skb %p", skb);
113 __skb_queue_tail(&sk->sk_receive_queue, skb);
114 spin_unlock_bh(&sk->sk_receive_queue.lock);
116 if (!sock_flag(sk, SOCK_DEAD))
117 sk->sk_data_ready(sk, skb_len);
121 spin_unlock_bh(&sk->sk_receive_queue.lock);
126 /* release the socket buffer */
128 skb->destructor = NULL;
130 rxrpc_put_call(call);
134 _leave(" = %d", ret);
139 * process a DATA packet, posting the packet to the appropriate queue
140 * - eats the packet if successful
142 static int rxrpc_fast_process_data(struct rxrpc_call *call,
143 struct sk_buff *skb, u32 seq)
145 struct rxrpc_skb_priv *sp;
147 int ret, ackbit, ack;
149 _enter("{%u,%u},,{%u}", call->rx_data_post, call->rx_first_oos, seq);
152 ASSERTCMP(sp->call, ==, NULL);
154 spin_lock(&call->lock);
156 if (call->state > RXRPC_CALL_COMPLETE)
159 ASSERTCMP(call->rx_data_expect, >=, call->rx_data_post);
160 ASSERTCMP(call->rx_data_post, >=, call->rx_data_recv);
161 ASSERTCMP(call->rx_data_recv, >=, call->rx_data_eaten);
163 if (seq < call->rx_data_post) {
164 _debug("dup #%u [-%u]", seq, call->rx_data_post);
165 ack = RXRPC_ACK_DUPLICATE;
167 goto discard_and_ack;
170 /* we may already have the packet in the out of sequence queue */
171 ackbit = seq - (call->rx_data_eaten + 1);
172 ASSERTCMP(ackbit, >=, 0);
173 if (__test_and_set_bit(ackbit, call->ackr_window)) {
174 _debug("dup oos #%u [%u,%u]",
175 seq, call->rx_data_eaten, call->rx_data_post);
176 ack = RXRPC_ACK_DUPLICATE;
177 goto discard_and_ack;
180 if (seq >= call->ackr_win_top) {
181 _debug("exceed #%u [%u]", seq, call->ackr_win_top);
182 __clear_bit(ackbit, call->ackr_window);
183 ack = RXRPC_ACK_EXCEEDS_WINDOW;
184 goto discard_and_ack;
187 if (seq == call->rx_data_expect) {
188 clear_bit(RXRPC_CALL_EXPECT_OOS, &call->flags);
189 call->rx_data_expect++;
190 } else if (seq > call->rx_data_expect) {
191 _debug("oos #%u [%u]", seq, call->rx_data_expect);
192 call->rx_data_expect = seq + 1;
193 if (test_and_set_bit(RXRPC_CALL_EXPECT_OOS, &call->flags)) {
194 ack = RXRPC_ACK_OUT_OF_SEQUENCE;
195 goto enqueue_and_ack;
200 if (seq != call->rx_data_post) {
201 _debug("ahead #%u [%u]", seq, call->rx_data_post);
205 if (test_bit(RXRPC_CALL_RCVD_LAST, &call->flags))
208 /* if the packet need security things doing to it, then it goes down
210 if (call->conn->security)
214 rxrpc_get_call(call);
215 terminal = ((sp->hdr.flags & RXRPC_LAST_PACKET) &&
216 !(sp->hdr.flags & RXRPC_CLIENT_INITIATED));
217 ret = rxrpc_queue_rcv_skb(call, skb, false, terminal);
219 if (ret == -ENOMEM || ret == -ENOBUFS) {
220 __clear_bit(ackbit, call->ackr_window);
221 ack = RXRPC_ACK_NOSPACE;
222 goto discard_and_ack;
229 _debug("post #%u", seq);
230 ASSERTCMP(call->rx_data_post, ==, seq);
231 call->rx_data_post++;
233 if (sp->hdr.flags & RXRPC_LAST_PACKET)
234 set_bit(RXRPC_CALL_RCVD_LAST, &call->flags);
236 /* if we've reached an out of sequence packet then we need to drain
237 * that queue into the socket Rx queue now */
238 if (call->rx_data_post == call->rx_first_oos) {
239 _debug("drain rx oos now");
240 read_lock(&call->state_lock);
241 if (call->state < RXRPC_CALL_COMPLETE &&
242 !test_and_set_bit(RXRPC_CALL_DRAIN_RX_OOS, &call->events))
243 rxrpc_queue_call(call);
244 read_unlock(&call->state_lock);
247 spin_unlock(&call->lock);
248 atomic_inc(&call->ackr_not_idle);
249 rxrpc_propose_ACK(call, RXRPC_ACK_DELAY, sp->hdr.serial, false);
250 _leave(" = 0 [posted]");
256 spin_unlock(&call->lock);
257 _leave(" = %d", ret);
261 _debug("discard and ACK packet %p", skb);
262 __rxrpc_propose_ACK(call, ack, sp->hdr.serial, true);
264 spin_unlock(&call->lock);
266 _leave(" = 0 [discarded]");
270 __rxrpc_propose_ACK(call, ack, sp->hdr.serial, true);
272 _net("defer skb %p", skb);
273 spin_unlock(&call->lock);
274 skb_queue_tail(&call->rx_queue, skb);
275 atomic_inc(&call->ackr_not_idle);
276 read_lock(&call->state_lock);
277 if (call->state < RXRPC_CALL_DEAD)
278 rxrpc_queue_call(call);
279 read_unlock(&call->state_lock);
280 _leave(" = 0 [queued]");
285 * assume an implicit ACKALL of the transmission phase of a client socket upon
286 * reception of the first reply packet
288 static void rxrpc_assume_implicit_ackall(struct rxrpc_call *call, u32 serial)
290 write_lock_bh(&call->state_lock);
292 switch (call->state) {
293 case RXRPC_CALL_CLIENT_AWAIT_REPLY:
294 call->state = RXRPC_CALL_CLIENT_RECV_REPLY;
295 call->acks_latest = serial;
297 _debug("implicit ACKALL %%%u", call->acks_latest);
298 set_bit(RXRPC_CALL_RCVD_ACKALL, &call->events);
299 write_unlock_bh(&call->state_lock);
301 if (try_to_del_timer_sync(&call->resend_timer) >= 0) {
302 clear_bit(RXRPC_CALL_RESEND_TIMER, &call->events);
303 clear_bit(RXRPC_CALL_RESEND, &call->events);
304 clear_bit(RXRPC_CALL_RUN_RTIMER, &call->flags);
309 write_unlock_bh(&call->state_lock);
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
318 void rxrpc_fast_process_packet(struct rxrpc_call *call, struct sk_buff *skb)
320 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
322 u32 serial, hi_serial, seq, abort_code;
324 _enter("%p,%p", call, skb);
326 ASSERT(!irqs_disabled());
328 #if 0 // INJECT RX ERROR
329 if (sp->hdr.type == RXRPC_PACKET_TYPE_DATA) {
332 printk("DROPPED 3RD PACKET!!!!!!!!!!!!!\n");
339 /* track the latest serial number on this connection for ACK packet
341 serial = ntohl(sp->hdr.serial);
342 hi_serial = atomic_read(&call->conn->hi_serial);
343 while (serial > hi_serial)
344 hi_serial = atomic_cmpxchg(&call->conn->hi_serial, hi_serial,
347 /* request ACK generation for any ACK or DATA packet that requests
349 if (sp->hdr.flags & RXRPC_REQUEST_ACK) {
350 _proto("ACK Requested on %%%u", serial);
351 rxrpc_propose_ACK(call, RXRPC_ACK_REQUESTED, sp->hdr.serial,
352 !(sp->hdr.flags & RXRPC_MORE_PACKETS));
355 switch (sp->hdr.type) {
356 case RXRPC_PACKET_TYPE_ABORT:
359 if (skb_copy_bits(skb, 0, &_abort_code,
360 sizeof(_abort_code)) < 0)
363 abort_code = ntohl(_abort_code);
364 _proto("Rx ABORT %%%u { %x }", serial, abort_code);
366 write_lock_bh(&call->state_lock);
367 if (call->state < RXRPC_CALL_COMPLETE) {
368 call->state = RXRPC_CALL_REMOTELY_ABORTED;
369 call->abort_code = abort_code;
370 set_bit(RXRPC_CALL_RCVD_ABORT, &call->events);
371 rxrpc_queue_call(call);
373 goto free_packet_unlock;
375 case RXRPC_PACKET_TYPE_BUSY:
376 _proto("Rx BUSY %%%u", serial);
378 if (call->conn->out_clientflag)
381 write_lock_bh(&call->state_lock);
382 switch (call->state) {
383 case RXRPC_CALL_CLIENT_SEND_REQUEST:
384 call->state = RXRPC_CALL_SERVER_BUSY;
385 set_bit(RXRPC_CALL_RCVD_BUSY, &call->events);
386 rxrpc_queue_call(call);
387 case RXRPC_CALL_SERVER_BUSY:
388 goto free_packet_unlock;
390 goto protocol_error_locked;
394 _proto("Rx %s %%%u", rxrpc_pkts[sp->hdr.type], serial);
397 case RXRPC_PACKET_TYPE_DATA:
398 seq = ntohl(sp->hdr.seq);
400 _proto("Rx DATA %%%u { #%u }", serial, seq);
405 call->ackr_prev_seq = sp->hdr.seq;
407 /* received data implicitly ACKs all of the request packets we
408 * sent when we're acting as a client */
409 if (call->state == RXRPC_CALL_CLIENT_AWAIT_REPLY)
410 rxrpc_assume_implicit_ackall(call, serial);
412 switch (rxrpc_fast_process_data(call, skb, seq)) {
420 /* data packet received beyond the last packet */
425 case RXRPC_PACKET_TYPE_ACK:
426 /* ACK processing is done in process context */
427 read_lock_bh(&call->state_lock);
428 if (call->state < RXRPC_CALL_DEAD) {
429 skb_queue_tail(&call->rx_queue, skb);
430 rxrpc_queue_call(call);
433 read_unlock_bh(&call->state_lock);
438 _debug("protocol error");
439 write_lock_bh(&call->state_lock);
440 protocol_error_locked:
441 if (call->state <= RXRPC_CALL_COMPLETE) {
442 call->state = RXRPC_CALL_LOCALLY_ABORTED;
443 call->abort_code = RX_PROTOCOL_ERROR;
444 set_bit(RXRPC_CALL_ABORT, &call->events);
445 rxrpc_queue_call(call);
448 write_unlock_bh(&call->state_lock);
456 * split up a jumbo data packet
458 static void rxrpc_process_jumbo_packet(struct rxrpc_call *call,
459 struct sk_buff *jumbo)
461 struct rxrpc_jumbo_header jhdr;
462 struct rxrpc_skb_priv *sp;
463 struct sk_buff *part;
465 _enter(",{%u,%u}", jumbo->data_len, jumbo->len);
467 sp = rxrpc_skb(jumbo);
470 sp->hdr.flags &= ~RXRPC_JUMBO_PACKET;
472 /* make a clone to represent the first subpacket in what's left
473 * of the jumbo packet */
474 part = skb_clone(jumbo, GFP_ATOMIC);
476 /* simply ditch the tail in the event of ENOMEM */
477 pskb_trim(jumbo, RXRPC_JUMBO_DATALEN);
482 pskb_trim(part, RXRPC_JUMBO_DATALEN);
484 if (!pskb_pull(jumbo, RXRPC_JUMBO_DATALEN))
487 if (skb_copy_bits(jumbo, 0, &jhdr, sizeof(jhdr)) < 0)
489 if (!pskb_pull(jumbo, sizeof(jhdr)))
492 sp->hdr.seq = htonl(ntohl(sp->hdr.seq) + 1);
493 sp->hdr.serial = htonl(ntohl(sp->hdr.serial) + 1);
494 sp->hdr.flags = jhdr.flags;
495 sp->hdr._rsvd = jhdr._rsvd;
497 _proto("Rx DATA Jumbo %%%u", ntohl(sp->hdr.serial) - 1);
499 rxrpc_fast_process_packet(call, part);
502 } while (sp->hdr.flags & RXRPC_JUMBO_PACKET);
504 rxrpc_fast_process_packet(call, jumbo);
509 _debug("protocol error");
510 rxrpc_free_skb(part);
511 rxrpc_free_skb(jumbo);
512 write_lock_bh(&call->state_lock);
513 if (call->state <= RXRPC_CALL_COMPLETE) {
514 call->state = RXRPC_CALL_LOCALLY_ABORTED;
515 call->abort_code = RX_PROTOCOL_ERROR;
516 set_bit(RXRPC_CALL_ABORT, &call->events);
517 rxrpc_queue_call(call);
519 write_unlock_bh(&call->state_lock);
524 * post an incoming packet to the appropriate call/socket to deal with
525 * - must get rid of the sk_buff, either by freeing it or by queuing it
527 static void rxrpc_post_packet_to_call(struct rxrpc_connection *conn,
530 struct rxrpc_skb_priv *sp;
531 struct rxrpc_call *call;
535 _enter("%p,%p", conn, skb);
537 read_lock_bh(&conn->lock);
541 /* look at extant calls by channel number first */
542 call = conn->channels[ntohl(sp->hdr.cid) & RXRPC_CHANNELMASK];
543 if (!call || call->call_id != sp->hdr.callNumber)
544 goto call_not_extant;
546 _debug("extant call [%d]", call->state);
547 ASSERTCMP(call->conn, ==, conn);
549 read_lock(&call->state_lock);
550 switch (call->state) {
551 case RXRPC_CALL_LOCALLY_ABORTED:
552 if (!test_and_set_bit(RXRPC_CALL_ABORT, &call->events))
553 rxrpc_queue_call(call);
554 case RXRPC_CALL_REMOTELY_ABORTED:
555 case RXRPC_CALL_NETWORK_ERROR:
556 case RXRPC_CALL_DEAD:
562 read_unlock(&call->state_lock);
563 rxrpc_get_call(call);
564 read_unlock_bh(&conn->lock);
566 if (sp->hdr.type == RXRPC_PACKET_TYPE_DATA &&
567 sp->hdr.flags & RXRPC_JUMBO_PACKET)
568 rxrpc_process_jumbo_packet(call, skb);
570 rxrpc_fast_process_packet(call, skb);
572 rxrpc_put_call(call);
576 /* search the completed calls in case what we're dealing with is
578 _debug("call not extant");
580 call_id = sp->hdr.callNumber;
581 p = conn->calls.rb_node;
583 call = rb_entry(p, struct rxrpc_call, conn_node);
585 if (call_id < call->call_id)
587 else if (call_id > call->call_id)
590 goto found_completed_call;
594 /* it's a either a really old call that we no longer remember or its a
595 * new incoming call */
596 read_unlock_bh(&conn->lock);
598 if (sp->hdr.flags & RXRPC_CLIENT_INITIATED &&
599 sp->hdr.seq == cpu_to_be32(1)) {
600 _debug("incoming call");
601 skb_queue_tail(&conn->trans->local->accept_queue, skb);
602 rxrpc_queue_work(&conn->trans->local->acceptor);
607 skb->priority = RX_CALL_DEAD;
608 rxrpc_reject_packet(conn->trans->local, skb);
611 /* resend last packet of a completed call
612 * - client calls may have been aborted or ACK'd
613 * - server calls may have been aborted
615 found_completed_call:
616 _debug("completed call");
618 if (atomic_read(&call->usage) == 0)
621 /* synchronise any state changes */
622 read_lock(&call->state_lock);
623 ASSERTIFCMP(call->state != RXRPC_CALL_CLIENT_FINAL_ACK,
624 call->state, >=, RXRPC_CALL_COMPLETE);
626 if (call->state == RXRPC_CALL_LOCALLY_ABORTED ||
627 call->state == RXRPC_CALL_REMOTELY_ABORTED ||
628 call->state == RXRPC_CALL_DEAD) {
629 read_unlock(&call->state_lock);
633 if (call->conn->in_clientflag) {
634 read_unlock(&call->state_lock);
635 goto dead_call; /* complete server call */
638 _debug("final ack again");
639 rxrpc_get_call(call);
640 set_bit(RXRPC_CALL_ACK_FINAL, &call->events);
641 rxrpc_queue_call(call);
644 read_unlock(&call->state_lock);
645 read_unlock_bh(&conn->lock);
652 * post connection-level events to the connection
653 * - this includes challenges, responses and some aborts
655 static void rxrpc_post_packet_to_conn(struct rxrpc_connection *conn,
658 _enter("%p,%p", conn, skb);
660 atomic_inc(&conn->usage);
661 skb_queue_tail(&conn->rx_queue, skb);
662 rxrpc_queue_conn(conn);
666 * handle data received on the local endpoint
667 * - may be called in interrupt context
669 void rxrpc_data_ready(struct sock *sk, int count)
671 struct rxrpc_connection *conn;
672 struct rxrpc_transport *trans;
673 struct rxrpc_skb_priv *sp;
674 struct rxrpc_local *local;
675 struct rxrpc_peer *peer;
679 _enter("%p, %d", sk, count);
681 ASSERT(!irqs_disabled());
683 read_lock_bh(&rxrpc_local_lock);
684 local = sk->sk_user_data;
685 if (local && atomic_read(&local->usage) > 0)
686 rxrpc_get_local(local);
689 read_unlock_bh(&rxrpc_local_lock);
691 _leave(" [local dead]");
695 skb = skb_recv_datagram(sk, 0, 1, &ret);
697 rxrpc_put_local(local);
700 _debug("UDP socket error %d", ret);
706 _net("recv skb %p", skb);
708 /* we'll probably need to checksum it (didn't call sock_recvmsg) */
709 if (skb_checksum_complete(skb)) {
711 rxrpc_put_local(local);
712 UDP_INC_STATS_BH(&init_net, UDP_MIB_INERRORS, 0);
713 _leave(" [CSUM failed]");
717 UDP_INC_STATS_BH(&init_net, UDP_MIB_INDATAGRAMS, 0);
719 /* the socket buffer we have is owned by UDP, with UDP's data all over
720 * it, but we really want our own */
723 memset(sp, 0, sizeof(*sp));
725 _net("Rx UDP packet from %08x:%04hu",
726 ntohl(ip_hdr(skb)->saddr), ntohs(udp_hdr(skb)->source));
728 /* dig out the RxRPC connection details */
729 if (skb_copy_bits(skb, sizeof(struct udphdr), &sp->hdr,
730 sizeof(sp->hdr)) < 0)
732 if (!pskb_pull(skb, sizeof(struct udphdr) + sizeof(sp->hdr)))
735 _net("Rx RxRPC %s ep=%x call=%x:%x",
736 sp->hdr.flags & RXRPC_CLIENT_INITIATED ? "ToServer" : "ToClient",
737 ntohl(sp->hdr.epoch),
739 ntohl(sp->hdr.callNumber));
741 if (sp->hdr.type == 0 || sp->hdr.type >= RXRPC_N_PACKET_TYPES) {
742 _proto("Rx Bad Packet Type %u", sp->hdr.type);
746 if (sp->hdr.type == RXRPC_PACKET_TYPE_DATA &&
747 (sp->hdr.callNumber == 0 || sp->hdr.seq == 0))
750 peer = rxrpc_find_peer(local, ip_hdr(skb)->saddr, udp_hdr(skb)->source);
752 goto cant_route_call;
754 trans = rxrpc_find_transport(local, peer);
755 rxrpc_put_peer(peer);
757 goto cant_route_call;
759 conn = rxrpc_find_connection(trans, &sp->hdr);
760 rxrpc_put_transport(trans);
762 goto cant_route_call;
764 _debug("CONN %p {%d}", conn, conn->debug_id);
766 if (sp->hdr.callNumber == 0)
767 rxrpc_post_packet_to_conn(conn, skb);
769 rxrpc_post_packet_to_call(conn, skb);
770 rxrpc_put_connection(conn);
771 rxrpc_put_local(local);
775 _debug("can't route call");
776 if (sp->hdr.flags & RXRPC_CLIENT_INITIATED &&
777 sp->hdr.type == RXRPC_PACKET_TYPE_DATA) {
778 if (sp->hdr.seq == cpu_to_be32(1)) {
779 _debug("first packet");
780 skb_queue_tail(&local->accept_queue, skb);
781 rxrpc_queue_work(&local->acceptor);
782 rxrpc_put_local(local);
783 _leave(" [incoming]");
786 skb->priority = RX_INVALID_OPERATION;
788 skb->priority = RX_CALL_DEAD;
792 rxrpc_reject_packet(local, skb);
793 rxrpc_put_local(local);
794 _leave(" [no call]");
798 skb->priority = RX_PROTOCOL_ERROR;
799 rxrpc_reject_packet(local, skb);
800 rxrpc_put_local(local);