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>
23 #include "ar-internal.h"
25 unsigned long rxrpc_ack_timeout = 1;
27 const char *rxrpc_pkts[] = {
29 "DATA", "ACK", "BUSY", "ABORT", "ACKALL", "CHALL", "RESP", "DEBUG",
30 "?09", "?10", "?11", "?12", "?13", "?14", "?15"
34 * queue a packet for recvmsg to pass to userspace
35 * - the caller must hold a lock on call->lock
36 * - must not be called with interrupts disabled (sk_filter() disables BH's)
37 * - eats the packet whether successful or not
38 * - there must be just one reference to the packet, which the caller passes to
41 int rxrpc_queue_rcv_skb(struct rxrpc_call *call, struct sk_buff *skb,
42 bool force, bool terminal)
44 struct rxrpc_skb_priv *sp;
45 struct rxrpc_sock *rx = call->socket;
49 _enter(",,%d,%d", force, terminal);
51 ASSERT(!irqs_disabled());
54 ASSERTCMP(sp->call, ==, call);
56 /* if we've already posted the terminal message for a call, then we
57 * don't post any more */
58 if (test_bit(RXRPC_CALL_TERMINAL_MSG, &call->flags)) {
59 _debug("already terminated");
60 ASSERTCMP(call->state, >=, RXRPC_CALL_COMPLETE);
61 skb->destructor = NULL;
71 /* cast skb->rcvbuf to unsigned... It's pointless, but
72 * reduces number of warnings when compiling with -W
75 // if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >=
76 // (unsigned) sk->sk_rcvbuf)
79 ret = sk_filter(sk, skb);
84 spin_lock_bh(&sk->sk_receive_queue.lock);
85 if (!test_bit(RXRPC_CALL_TERMINAL_MSG, &call->flags) &&
86 !test_bit(RXRPC_CALL_RELEASED, &call->flags) &&
87 call->socket->sk.sk_state != RXRPC_CLOSE) {
88 skb->destructor = rxrpc_packet_destructor;
91 atomic_add(skb->truesize, &sk->sk_rmem_alloc);
94 _debug("<<<< TERMINAL MESSAGE >>>>");
95 set_bit(RXRPC_CALL_TERMINAL_MSG, &call->flags);
98 /* allow interception by a kernel service */
99 if (rx->interceptor) {
100 rx->interceptor(sk, call->user_call_ID, skb);
101 spin_unlock_bh(&sk->sk_receive_queue.lock);
104 /* Cache the SKB length before we tack it onto the
105 * receive queue. Once it is added it no longer
106 * belongs to us and may be freed by other threads of
107 * control pulling packets from the queue */
110 _net("post skb %p", skb);
111 __skb_queue_tail(&sk->sk_receive_queue, skb);
112 spin_unlock_bh(&sk->sk_receive_queue.lock);
114 if (!sock_flag(sk, SOCK_DEAD))
115 sk->sk_data_ready(sk, skb_len);
119 spin_unlock_bh(&sk->sk_receive_queue.lock);
124 /* release the socket buffer */
126 skb->destructor = NULL;
128 rxrpc_put_call(call);
132 _leave(" = %d", ret);
137 * process a DATA packet, posting the packet to the appropriate queue
138 * - eats the packet if successful
140 static int rxrpc_fast_process_data(struct rxrpc_call *call,
141 struct sk_buff *skb, u32 seq)
143 struct rxrpc_skb_priv *sp;
145 int ret, ackbit, ack;
147 _enter("{%u,%u},,{%u}", call->rx_data_post, call->rx_first_oos, seq);
150 ASSERTCMP(sp->call, ==, NULL);
152 spin_lock(&call->lock);
154 if (call->state > RXRPC_CALL_COMPLETE)
157 ASSERTCMP(call->rx_data_expect, >=, call->rx_data_post);
158 ASSERTCMP(call->rx_data_post, >=, call->rx_data_recv);
159 ASSERTCMP(call->rx_data_recv, >=, call->rx_data_eaten);
161 if (seq < call->rx_data_post) {
162 _debug("dup #%u [-%u]", seq, call->rx_data_post);
163 ack = RXRPC_ACK_DUPLICATE;
165 goto discard_and_ack;
168 /* we may already have the packet in the out of sequence queue */
169 ackbit = seq - (call->rx_data_eaten + 1);
170 ASSERTCMP(ackbit, >=, 0);
171 if (__test_and_set_bit(ackbit, call->ackr_window)) {
172 _debug("dup oos #%u [%u,%u]",
173 seq, call->rx_data_eaten, call->rx_data_post);
174 ack = RXRPC_ACK_DUPLICATE;
175 goto discard_and_ack;
178 if (seq >= call->ackr_win_top) {
179 _debug("exceed #%u [%u]", seq, call->ackr_win_top);
180 __clear_bit(ackbit, call->ackr_window);
181 ack = RXRPC_ACK_EXCEEDS_WINDOW;
182 goto discard_and_ack;
185 if (seq == call->rx_data_expect) {
186 clear_bit(RXRPC_CALL_EXPECT_OOS, &call->flags);
187 call->rx_data_expect++;
188 } else if (seq > call->rx_data_expect) {
189 _debug("oos #%u [%u]", seq, call->rx_data_expect);
190 call->rx_data_expect = seq + 1;
191 if (test_and_set_bit(RXRPC_CALL_EXPECT_OOS, &call->flags)) {
192 ack = RXRPC_ACK_OUT_OF_SEQUENCE;
193 goto enqueue_and_ack;
198 if (seq != call->rx_data_post) {
199 _debug("ahead #%u [%u]", seq, call->rx_data_post);
203 if (test_bit(RXRPC_CALL_RCVD_LAST, &call->flags))
206 /* if the packet need security things doing to it, then it goes down
208 if (call->conn->security)
212 rxrpc_get_call(call);
213 terminal = ((sp->hdr.flags & RXRPC_LAST_PACKET) &&
214 !(sp->hdr.flags & RXRPC_CLIENT_INITIATED));
215 ret = rxrpc_queue_rcv_skb(call, skb, false, terminal);
217 if (ret == -ENOMEM || ret == -ENOBUFS) {
218 __clear_bit(ackbit, call->ackr_window);
219 ack = RXRPC_ACK_NOSPACE;
220 goto discard_and_ack;
227 _debug("post #%u", seq);
228 ASSERTCMP(call->rx_data_post, ==, seq);
229 call->rx_data_post++;
231 if (sp->hdr.flags & RXRPC_LAST_PACKET)
232 set_bit(RXRPC_CALL_RCVD_LAST, &call->flags);
234 /* if we've reached an out of sequence packet then we need to drain
235 * that queue into the socket Rx queue now */
236 if (call->rx_data_post == call->rx_first_oos) {
237 _debug("drain rx oos now");
238 read_lock(&call->state_lock);
239 if (call->state < RXRPC_CALL_COMPLETE &&
240 !test_and_set_bit(RXRPC_CALL_DRAIN_RX_OOS, &call->events))
241 rxrpc_queue_call(call);
242 read_unlock(&call->state_lock);
245 spin_unlock(&call->lock);
246 atomic_inc(&call->ackr_not_idle);
247 rxrpc_propose_ACK(call, RXRPC_ACK_DELAY, sp->hdr.serial, false);
248 _leave(" = 0 [posted]");
254 spin_unlock(&call->lock);
255 _leave(" = %d", ret);
259 _debug("discard and ACK packet %p", skb);
260 __rxrpc_propose_ACK(call, ack, sp->hdr.serial, true);
262 spin_unlock(&call->lock);
264 _leave(" = 0 [discarded]");
268 __rxrpc_propose_ACK(call, ack, sp->hdr.serial, true);
270 _net("defer skb %p", skb);
271 spin_unlock(&call->lock);
272 skb_queue_tail(&call->rx_queue, skb);
273 atomic_inc(&call->ackr_not_idle);
274 read_lock(&call->state_lock);
275 if (call->state < RXRPC_CALL_DEAD)
276 rxrpc_queue_call(call);
277 read_unlock(&call->state_lock);
278 _leave(" = 0 [queued]");
283 * assume an implicit ACKALL of the transmission phase of a client socket upon
284 * reception of the first reply packet
286 static void rxrpc_assume_implicit_ackall(struct rxrpc_call *call, u32 serial)
288 write_lock_bh(&call->state_lock);
290 switch (call->state) {
291 case RXRPC_CALL_CLIENT_AWAIT_REPLY:
292 call->state = RXRPC_CALL_CLIENT_RECV_REPLY;
293 call->acks_latest = serial;
295 _debug("implicit ACKALL %%%u", call->acks_latest);
296 set_bit(RXRPC_CALL_RCVD_ACKALL, &call->events);
297 write_unlock_bh(&call->state_lock);
299 if (try_to_del_timer_sync(&call->resend_timer) >= 0) {
300 clear_bit(RXRPC_CALL_RESEND_TIMER, &call->events);
301 clear_bit(RXRPC_CALL_RESEND, &call->events);
302 clear_bit(RXRPC_CALL_RUN_RTIMER, &call->flags);
307 write_unlock_bh(&call->state_lock);
313 * post an incoming packet to the nominated call to deal with
314 * - must get rid of the sk_buff, either by freeing it or by queuing it
316 void rxrpc_fast_process_packet(struct rxrpc_call *call, struct sk_buff *skb)
318 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
320 u32 serial, hi_serial, seq, abort_code;
322 _enter("%p,%p", call, skb);
324 ASSERT(!irqs_disabled());
326 #if 0 // INJECT RX ERROR
327 if (sp->hdr.type == RXRPC_PACKET_TYPE_DATA) {
330 printk("DROPPED 3RD PACKET!!!!!!!!!!!!!\n");
337 /* track the latest serial number on this connection for ACK packet
339 serial = ntohl(sp->hdr.serial);
340 hi_serial = atomic_read(&call->conn->hi_serial);
341 while (serial > hi_serial)
342 hi_serial = atomic_cmpxchg(&call->conn->hi_serial, hi_serial,
345 /* request ACK generation for any ACK or DATA packet that requests
347 if (sp->hdr.flags & RXRPC_REQUEST_ACK) {
348 _proto("ACK Requested on %%%u", serial);
349 rxrpc_propose_ACK(call, RXRPC_ACK_REQUESTED, sp->hdr.serial,
350 !(sp->hdr.flags & RXRPC_MORE_PACKETS));
353 switch (sp->hdr.type) {
354 case RXRPC_PACKET_TYPE_ABORT:
357 if (skb_copy_bits(skb, 0, &_abort_code,
358 sizeof(_abort_code)) < 0)
361 abort_code = ntohl(_abort_code);
362 _proto("Rx ABORT %%%u { %x }", serial, abort_code);
364 write_lock_bh(&call->state_lock);
365 if (call->state < RXRPC_CALL_COMPLETE) {
366 call->state = RXRPC_CALL_REMOTELY_ABORTED;
367 call->abort_code = abort_code;
368 set_bit(RXRPC_CALL_RCVD_ABORT, &call->events);
369 rxrpc_queue_call(call);
371 goto free_packet_unlock;
373 case RXRPC_PACKET_TYPE_BUSY:
374 _proto("Rx BUSY %%%u", serial);
376 if (call->conn->out_clientflag)
379 write_lock_bh(&call->state_lock);
380 switch (call->state) {
381 case RXRPC_CALL_CLIENT_SEND_REQUEST:
382 call->state = RXRPC_CALL_SERVER_BUSY;
383 set_bit(RXRPC_CALL_RCVD_BUSY, &call->events);
384 rxrpc_queue_call(call);
385 case RXRPC_CALL_SERVER_BUSY:
386 goto free_packet_unlock;
388 goto protocol_error_locked;
392 _proto("Rx %s %%%u", rxrpc_pkts[sp->hdr.type], serial);
395 case RXRPC_PACKET_TYPE_DATA:
396 seq = ntohl(sp->hdr.seq);
398 _proto("Rx DATA %%%u { #%u }", serial, seq);
403 call->ackr_prev_seq = sp->hdr.seq;
405 /* received data implicitly ACKs all of the request packets we
406 * sent when we're acting as a client */
407 if (call->state == RXRPC_CALL_CLIENT_AWAIT_REPLY)
408 rxrpc_assume_implicit_ackall(call, serial);
410 switch (rxrpc_fast_process_data(call, skb, seq)) {
418 /* data packet received beyond the last packet */
423 case RXRPC_PACKET_TYPE_ACK:
424 /* ACK processing is done in process context */
425 read_lock_bh(&call->state_lock);
426 if (call->state < RXRPC_CALL_DEAD) {
427 skb_queue_tail(&call->rx_queue, skb);
428 rxrpc_queue_call(call);
431 read_unlock_bh(&call->state_lock);
436 _debug("protocol error");
437 write_lock_bh(&call->state_lock);
438 protocol_error_locked:
439 if (call->state <= RXRPC_CALL_COMPLETE) {
440 call->state = RXRPC_CALL_LOCALLY_ABORTED;
441 call->abort_code = RX_PROTOCOL_ERROR;
442 set_bit(RXRPC_CALL_ABORT, &call->events);
443 rxrpc_queue_call(call);
446 write_unlock_bh(&call->state_lock);
454 * split up a jumbo data packet
456 static void rxrpc_process_jumbo_packet(struct rxrpc_call *call,
457 struct sk_buff *jumbo)
459 struct rxrpc_jumbo_header jhdr;
460 struct rxrpc_skb_priv *sp;
461 struct sk_buff *part;
463 _enter(",{%u,%u}", jumbo->data_len, jumbo->len);
465 sp = rxrpc_skb(jumbo);
468 sp->hdr.flags &= ~RXRPC_JUMBO_PACKET;
470 /* make a clone to represent the first subpacket in what's left
471 * of the jumbo packet */
472 part = skb_clone(jumbo, GFP_ATOMIC);
474 /* simply ditch the tail in the event of ENOMEM */
475 pskb_trim(jumbo, RXRPC_JUMBO_DATALEN);
480 pskb_trim(part, RXRPC_JUMBO_DATALEN);
482 if (!pskb_pull(jumbo, RXRPC_JUMBO_DATALEN))
485 if (skb_copy_bits(jumbo, 0, &jhdr, sizeof(jhdr)) < 0)
487 if (!pskb_pull(jumbo, sizeof(jhdr)))
490 sp->hdr.seq = htonl(ntohl(sp->hdr.seq) + 1);
491 sp->hdr.serial = htonl(ntohl(sp->hdr.serial) + 1);
492 sp->hdr.flags = jhdr.flags;
493 sp->hdr._rsvd = jhdr._rsvd;
495 _proto("Rx DATA Jumbo %%%u", ntohl(sp->hdr.serial) - 1);
497 rxrpc_fast_process_packet(call, part);
500 } while (sp->hdr.flags & RXRPC_JUMBO_PACKET);
502 rxrpc_fast_process_packet(call, jumbo);
507 _debug("protocol error");
508 rxrpc_free_skb(part);
509 rxrpc_free_skb(jumbo);
510 write_lock_bh(&call->state_lock);
511 if (call->state <= RXRPC_CALL_COMPLETE) {
512 call->state = RXRPC_CALL_LOCALLY_ABORTED;
513 call->abort_code = RX_PROTOCOL_ERROR;
514 set_bit(RXRPC_CALL_ABORT, &call->events);
515 rxrpc_queue_call(call);
517 write_unlock_bh(&call->state_lock);
522 * post an incoming packet to the appropriate call/socket to deal with
523 * - must get rid of the sk_buff, either by freeing it or by queuing it
525 static void rxrpc_post_packet_to_call(struct rxrpc_connection *conn,
528 struct rxrpc_skb_priv *sp;
529 struct rxrpc_call *call;
533 _enter("%p,%p", conn, skb);
535 read_lock_bh(&conn->lock);
539 /* look at extant calls by channel number first */
540 call = conn->channels[ntohl(sp->hdr.cid) & RXRPC_CHANNELMASK];
541 if (!call || call->call_id != sp->hdr.callNumber)
542 goto call_not_extant;
544 _debug("extant call [%d]", call->state);
545 ASSERTCMP(call->conn, ==, conn);
547 read_lock(&call->state_lock);
548 switch (call->state) {
549 case RXRPC_CALL_LOCALLY_ABORTED:
550 if (!test_and_set_bit(RXRPC_CALL_ABORT, &call->events))
551 rxrpc_queue_call(call);
552 case RXRPC_CALL_REMOTELY_ABORTED:
553 case RXRPC_CALL_NETWORK_ERROR:
554 case RXRPC_CALL_DEAD:
560 read_unlock(&call->state_lock);
561 rxrpc_get_call(call);
562 read_unlock_bh(&conn->lock);
564 if (sp->hdr.type == RXRPC_PACKET_TYPE_DATA &&
565 sp->hdr.flags & RXRPC_JUMBO_PACKET)
566 rxrpc_process_jumbo_packet(call, skb);
568 rxrpc_fast_process_packet(call, skb);
570 rxrpc_put_call(call);
574 /* search the completed calls in case what we're dealing with is
576 _debug("call not extant");
578 call_id = sp->hdr.callNumber;
579 p = conn->calls.rb_node;
581 call = rb_entry(p, struct rxrpc_call, conn_node);
583 if (call_id < call->call_id)
585 else if (call_id > call->call_id)
588 goto found_completed_call;
592 /* it's a either a really old call that we no longer remember or its a
593 * new incoming call */
594 read_unlock_bh(&conn->lock);
596 if (sp->hdr.flags & RXRPC_CLIENT_INITIATED &&
597 sp->hdr.seq == __constant_cpu_to_be32(1)) {
598 _debug("incoming call");
599 skb_queue_tail(&conn->trans->local->accept_queue, skb);
600 rxrpc_queue_work(&conn->trans->local->acceptor);
605 skb->priority = RX_CALL_DEAD;
606 rxrpc_reject_packet(conn->trans->local, skb);
609 /* resend last packet of a completed call
610 * - client calls may have been aborted or ACK'd
611 * - server calls may have been aborted
613 found_completed_call:
614 _debug("completed call");
616 if (atomic_read(&call->usage) == 0)
619 /* synchronise any state changes */
620 read_lock(&call->state_lock);
621 ASSERTIFCMP(call->state != RXRPC_CALL_CLIENT_FINAL_ACK,
622 call->state, >=, RXRPC_CALL_COMPLETE);
624 if (call->state == RXRPC_CALL_LOCALLY_ABORTED ||
625 call->state == RXRPC_CALL_REMOTELY_ABORTED ||
626 call->state == RXRPC_CALL_DEAD) {
627 read_unlock(&call->state_lock);
631 if (call->conn->in_clientflag) {
632 read_unlock(&call->state_lock);
633 goto dead_call; /* complete server call */
636 _debug("final ack again");
637 rxrpc_get_call(call);
638 set_bit(RXRPC_CALL_ACK_FINAL, &call->events);
639 rxrpc_queue_call(call);
642 read_unlock(&call->state_lock);
643 read_unlock_bh(&conn->lock);
650 * post connection-level events to the connection
651 * - this includes challenges, responses and some aborts
653 static void rxrpc_post_packet_to_conn(struct rxrpc_connection *conn,
656 _enter("%p,%p", conn, skb);
658 atomic_inc(&conn->usage);
659 skb_queue_tail(&conn->rx_queue, skb);
660 rxrpc_queue_conn(conn);
664 * handle data received on the local endpoint
665 * - may be called in interrupt context
667 void rxrpc_data_ready(struct sock *sk, int count)
669 struct rxrpc_connection *conn;
670 struct rxrpc_transport *trans;
671 struct rxrpc_skb_priv *sp;
672 struct rxrpc_local *local;
673 struct rxrpc_peer *peer;
677 _enter("%p, %d", sk, count);
679 ASSERT(!irqs_disabled());
681 read_lock_bh(&rxrpc_local_lock);
682 local = sk->sk_user_data;
683 if (local && atomic_read(&local->usage) > 0)
684 rxrpc_get_local(local);
687 read_unlock_bh(&rxrpc_local_lock);
689 _leave(" [local dead]");
693 skb = skb_recv_datagram(sk, 0, 1, &ret);
695 rxrpc_put_local(local);
698 _debug("UDP socket error %d", ret);
704 _net("recv skb %p", skb);
706 /* we'll probably need to checksum it (didn't call sock_recvmsg) */
707 if (skb_checksum_complete(skb)) {
709 rxrpc_put_local(local);
710 _leave(" [CSUM failed]");
714 /* the socket buffer we have is owned by UDP, with UDP's data all over
715 * it, but we really want our own */
718 memset(sp, 0, sizeof(*sp));
720 _net("Rx UDP packet from %08x:%04hu",
721 ntohl(ip_hdr(skb)->saddr), ntohs(udp_hdr(skb)->source));
723 /* dig out the RxRPC connection details */
724 if (skb_copy_bits(skb, sizeof(struct udphdr), &sp->hdr,
725 sizeof(sp->hdr)) < 0)
727 if (!pskb_pull(skb, sizeof(struct udphdr) + sizeof(sp->hdr)))
730 _net("Rx RxRPC %s ep=%x call=%x:%x",
731 sp->hdr.flags & RXRPC_CLIENT_INITIATED ? "ToServer" : "ToClient",
732 ntohl(sp->hdr.epoch),
734 ntohl(sp->hdr.callNumber));
736 if (sp->hdr.type == 0 || sp->hdr.type >= RXRPC_N_PACKET_TYPES) {
737 _proto("Rx Bad Packet Type %u", sp->hdr.type);
741 if (sp->hdr.type == RXRPC_PACKET_TYPE_DATA &&
742 (sp->hdr.callNumber == 0 || sp->hdr.seq == 0))
745 peer = rxrpc_find_peer(local, ip_hdr(skb)->saddr, udp_hdr(skb)->source);
747 goto cant_route_call;
749 trans = rxrpc_find_transport(local, peer);
750 rxrpc_put_peer(peer);
752 goto cant_route_call;
754 conn = rxrpc_find_connection(trans, &sp->hdr);
755 rxrpc_put_transport(trans);
757 goto cant_route_call;
759 _debug("CONN %p {%d}", conn, conn->debug_id);
761 if (sp->hdr.callNumber == 0)
762 rxrpc_post_packet_to_conn(conn, skb);
764 rxrpc_post_packet_to_call(conn, skb);
765 rxrpc_put_connection(conn);
766 rxrpc_put_local(local);
770 _debug("can't route call");
771 if (sp->hdr.flags & RXRPC_CLIENT_INITIATED &&
772 sp->hdr.type == RXRPC_PACKET_TYPE_DATA) {
773 if (sp->hdr.seq == __constant_cpu_to_be32(1)) {
774 _debug("first packet");
775 skb_queue_tail(&local->accept_queue, skb);
776 rxrpc_queue_work(&local->acceptor);
777 rxrpc_put_local(local);
778 _leave(" [incoming]");
781 skb->priority = RX_INVALID_OPERATION;
783 skb->priority = RX_CALL_DEAD;
787 rxrpc_reject_packet(local, skb);
788 rxrpc_put_local(local);
789 _leave(" [no call]");
793 skb->priority = RX_PROTOCOL_ERROR;
794 rxrpc_reject_packet(local, skb);
795 rxrpc_put_local(local);