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 #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"
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
38 int rxrpc_queue_rcv_skb(struct rxrpc_call *call, struct sk_buff *skb,
39 bool force, bool terminal)
41 struct rxrpc_skb_priv *sp;
42 struct rxrpc_sock *rx = call->socket;
46 _enter(",,%d,%d", force, terminal);
48 ASSERT(!irqs_disabled());
51 ASSERTCMP(sp->call, ==, call);
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);
65 /* cast skb->rcvbuf to unsigned... It's pointless, but
66 * reduces number of warnings when compiling with -W
69 // if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >=
70 // (unsigned int) sk->sk_rcvbuf)
73 ret = sk_filter(sk, skb);
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;
85 atomic_add(skb->truesize, &sk->sk_rmem_alloc);
88 _debug("<<<< TERMINAL MESSAGE >>>>");
89 set_bit(RXRPC_CALL_TERMINAL_MSG, &call->flags);
92 /* allow interception by a kernel service */
93 if (skb->mark == RXRPC_SKB_MARK_NEW_CALL &&
94 rx->notify_new_call) {
95 spin_unlock_bh(&sk->sk_receive_queue.lock);
96 skb_queue_tail(&call->knlrecv_queue, skb);
97 rx->notify_new_call(&rx->sk);
98 } else if (call->notify_rx) {
99 spin_unlock_bh(&sk->sk_receive_queue.lock);
100 skb_queue_tail(&call->knlrecv_queue, skb);
101 call->notify_rx(&rx->sk, call, call->user_call_ID);
103 _net("post skb %p", skb);
104 __skb_queue_tail(&sk->sk_receive_queue, skb);
105 spin_unlock_bh(&sk->sk_receive_queue.lock);
107 if (!sock_flag(sk, SOCK_DEAD))
108 sk->sk_data_ready(sk);
112 spin_unlock_bh(&sk->sk_receive_queue.lock);
119 _leave(" = %d", ret);
124 * process a DATA packet, posting the packet to the appropriate queue
125 * - eats the packet if successful
127 static int rxrpc_fast_process_data(struct rxrpc_call *call,
128 struct sk_buff *skb, u32 seq)
130 struct rxrpc_skb_priv *sp;
132 int ret, ackbit, ack;
137 _enter("{%u,%u},,{%u}", call->rx_data_post, call->rx_first_oos, seq);
140 ASSERTCMP(sp->call, ==, NULL);
141 flags = sp->hdr.flags;
142 serial = sp->hdr.serial;
143 skew = skb->priority;
145 spin_lock(&call->lock);
147 if (call->state > RXRPC_CALL_COMPLETE)
150 ASSERTCMP(call->rx_data_expect, >=, call->rx_data_post);
151 ASSERTCMP(call->rx_data_post, >=, call->rx_data_recv);
152 ASSERTCMP(call->rx_data_recv, >=, call->rx_data_eaten);
154 if (seq < call->rx_data_post) {
155 _debug("dup #%u [-%u]", seq, call->rx_data_post);
156 ack = RXRPC_ACK_DUPLICATE;
158 goto discard_and_ack;
161 /* we may already have the packet in the out of sequence queue */
162 ackbit = seq - (call->rx_data_eaten + 1);
163 ASSERTCMP(ackbit, >=, 0);
164 if (__test_and_set_bit(ackbit, call->ackr_window)) {
165 _debug("dup oos #%u [%u,%u]",
166 seq, call->rx_data_eaten, call->rx_data_post);
167 ack = RXRPC_ACK_DUPLICATE;
168 goto discard_and_ack;
171 if (seq >= call->ackr_win_top) {
172 _debug("exceed #%u [%u]", seq, call->ackr_win_top);
173 __clear_bit(ackbit, call->ackr_window);
174 ack = RXRPC_ACK_EXCEEDS_WINDOW;
175 goto discard_and_ack;
178 if (seq == call->rx_data_expect) {
179 clear_bit(RXRPC_CALL_EXPECT_OOS, &call->flags);
180 call->rx_data_expect++;
181 } else if (seq > call->rx_data_expect) {
182 _debug("oos #%u [%u]", seq, call->rx_data_expect);
183 call->rx_data_expect = seq + 1;
184 if (test_and_set_bit(RXRPC_CALL_EXPECT_OOS, &call->flags)) {
185 ack = RXRPC_ACK_OUT_OF_SEQUENCE;
186 goto enqueue_and_ack;
191 if (seq != call->rx_data_post) {
192 _debug("ahead #%u [%u]", seq, call->rx_data_post);
196 if (test_bit(RXRPC_CALL_RCVD_LAST, &call->flags))
199 /* if the packet need security things doing to it, then it goes down
201 if (call->security_ix)
205 rxrpc_get_call_for_skb(call, skb);
206 terminal = ((flags & RXRPC_LAST_PACKET) &&
207 !(flags & RXRPC_CLIENT_INITIATED));
208 ret = rxrpc_queue_rcv_skb(call, skb, false, terminal);
210 if (ret == -ENOMEM || ret == -ENOBUFS) {
211 __clear_bit(ackbit, call->ackr_window);
212 ack = RXRPC_ACK_NOSPACE;
213 goto discard_and_ack;
221 _debug("post #%u", seq);
222 ASSERTCMP(call->rx_data_post, ==, seq);
223 call->rx_data_post++;
225 if (flags & RXRPC_LAST_PACKET)
226 set_bit(RXRPC_CALL_RCVD_LAST, &call->flags);
228 /* if we've reached an out of sequence packet then we need to drain
229 * that queue into the socket Rx queue now */
230 if (call->rx_data_post == call->rx_first_oos) {
231 _debug("drain rx oos now");
232 read_lock(&call->state_lock);
233 if (call->state < RXRPC_CALL_COMPLETE &&
234 !test_and_set_bit(RXRPC_CALL_EV_DRAIN_RX_OOS, &call->events))
235 rxrpc_queue_call(call);
236 read_unlock(&call->state_lock);
239 spin_unlock(&call->lock);
240 atomic_inc(&call->ackr_not_idle);
241 rxrpc_propose_ACK(call, RXRPC_ACK_DELAY, skew, serial, false);
242 _leave(" = 0 [posted]");
248 spin_unlock(&call->lock);
249 _leave(" = %d", ret);
253 _debug("discard and ACK packet %p", skb);
254 __rxrpc_propose_ACK(call, ack, skew, serial, true);
256 spin_unlock(&call->lock);
258 _leave(" = 0 [discarded]");
262 __rxrpc_propose_ACK(call, ack, skew, serial, true);
264 _net("defer skb %p", skb);
265 spin_unlock(&call->lock);
266 skb_queue_tail(&call->rx_queue, skb);
267 atomic_inc(&call->ackr_not_idle);
268 read_lock(&call->state_lock);
269 if (call->state < RXRPC_CALL_DEAD)
270 rxrpc_queue_call(call);
271 read_unlock(&call->state_lock);
272 _leave(" = 0 [queued]");
277 * assume an implicit ACKALL of the transmission phase of a client socket upon
278 * reception of the first reply packet
280 static void rxrpc_assume_implicit_ackall(struct rxrpc_call *call, u32 serial)
282 write_lock_bh(&call->state_lock);
284 switch (call->state) {
285 case RXRPC_CALL_CLIENT_AWAIT_REPLY:
286 call->state = RXRPC_CALL_CLIENT_RECV_REPLY;
287 call->acks_latest = serial;
289 _debug("implicit ACKALL %%%u", call->acks_latest);
290 set_bit(RXRPC_CALL_EV_RCVD_ACKALL, &call->events);
291 write_unlock_bh(&call->state_lock);
293 if (try_to_del_timer_sync(&call->resend_timer) >= 0) {
294 clear_bit(RXRPC_CALL_EV_RESEND_TIMER, &call->events);
295 clear_bit(RXRPC_CALL_EV_RESEND, &call->events);
296 clear_bit(RXRPC_CALL_RUN_RTIMER, &call->flags);
301 write_unlock_bh(&call->state_lock);
307 * post an incoming packet to the nominated call to deal with
308 * - must get rid of the sk_buff, either by freeing it or by queuing it
310 void rxrpc_fast_process_packet(struct rxrpc_call *call, struct sk_buff *skb)
312 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
316 _enter("%p,%p", call, skb);
318 ASSERT(!irqs_disabled());
320 #if 0 // INJECT RX ERROR
321 if (sp->hdr.type == RXRPC_PACKET_TYPE_DATA) {
324 printk("DROPPED 3RD PACKET!!!!!!!!!!!!!\n");
331 /* request ACK generation for any ACK or DATA packet that requests
333 if (sp->hdr.flags & RXRPC_REQUEST_ACK) {
334 _proto("ACK Requested on %%%u", sp->hdr.serial);
335 rxrpc_propose_ACK(call, RXRPC_ACK_REQUESTED,
336 skb->priority, sp->hdr.serial, false);
339 switch (sp->hdr.type) {
340 case RXRPC_PACKET_TYPE_ABORT:
343 if (skb_copy_bits(skb, 0, &wtmp, sizeof(wtmp)) < 0)
346 abort_code = ntohl(wtmp);
347 _proto("Rx ABORT %%%u { %x }", sp->hdr.serial, abort_code);
349 if (__rxrpc_set_call_completion(call,
350 RXRPC_CALL_REMOTELY_ABORTED,
351 abort_code, ECONNABORTED)) {
352 set_bit(RXRPC_CALL_EV_RCVD_ABORT, &call->events);
353 rxrpc_queue_call(call);
357 case RXRPC_PACKET_TYPE_BUSY:
358 _proto("Rx BUSY %%%u", sp->hdr.serial);
360 if (rxrpc_conn_is_service(call->conn))
363 write_lock_bh(&call->state_lock);
364 switch (call->state) {
365 case RXRPC_CALL_CLIENT_SEND_REQUEST:
366 __rxrpc_set_call_completion(call,
367 RXRPC_CALL_SERVER_BUSY,
369 set_bit(RXRPC_CALL_EV_RCVD_BUSY, &call->events);
370 rxrpc_queue_call(call);
371 case RXRPC_CALL_SERVER_BUSY:
372 goto free_packet_unlock;
374 goto protocol_error_locked;
378 _proto("Rx %s %%%u", rxrpc_pkts[sp->hdr.type], sp->hdr.serial);
381 case RXRPC_PACKET_TYPE_DATA:
382 _proto("Rx DATA %%%u { #%u }", sp->hdr.serial, sp->hdr.seq);
384 if (sp->hdr.seq == 0)
387 call->ackr_prev_seq = sp->hdr.seq;
389 /* received data implicitly ACKs all of the request packets we
390 * sent when we're acting as a client */
391 if (call->state == RXRPC_CALL_CLIENT_AWAIT_REPLY)
392 rxrpc_assume_implicit_ackall(call, sp->hdr.serial);
394 switch (rxrpc_fast_process_data(call, skb, sp->hdr.seq)) {
402 /* data packet received beyond the last packet */
407 case RXRPC_PACKET_TYPE_ACKALL:
408 case RXRPC_PACKET_TYPE_ACK:
409 /* ACK processing is done in process context */
410 read_lock_bh(&call->state_lock);
411 if (call->state < RXRPC_CALL_DEAD) {
412 skb_queue_tail(&call->rx_queue, skb);
413 rxrpc_queue_call(call);
416 read_unlock_bh(&call->state_lock);
421 _debug("protocol error");
422 write_lock_bh(&call->state_lock);
423 protocol_error_locked:
424 if (__rxrpc_abort_call(call, RX_PROTOCOL_ERROR, EPROTO))
425 rxrpc_queue_call(call);
427 write_unlock_bh(&call->state_lock);
435 * split up a jumbo data packet
437 static void rxrpc_process_jumbo_packet(struct rxrpc_call *call,
438 struct sk_buff *jumbo)
440 struct rxrpc_jumbo_header jhdr;
441 struct rxrpc_skb_priv *sp;
442 struct sk_buff *part;
444 _enter(",{%u,%u}", jumbo->data_len, jumbo->len);
446 sp = rxrpc_skb(jumbo);
449 sp->hdr.flags &= ~RXRPC_JUMBO_PACKET;
451 /* make a clone to represent the first subpacket in what's left
452 * of the jumbo packet */
453 part = skb_clone(jumbo, GFP_ATOMIC);
455 /* simply ditch the tail in the event of ENOMEM */
456 pskb_trim(jumbo, RXRPC_JUMBO_DATALEN);
461 pskb_trim(part, RXRPC_JUMBO_DATALEN);
463 if (!pskb_pull(jumbo, RXRPC_JUMBO_DATALEN))
466 if (skb_copy_bits(jumbo, 0, &jhdr, sizeof(jhdr)) < 0)
468 if (!pskb_pull(jumbo, sizeof(jhdr)))
473 sp->hdr.flags = jhdr.flags;
474 sp->hdr._rsvd = ntohs(jhdr._rsvd);
476 _proto("Rx DATA Jumbo %%%u", sp->hdr.serial - 1);
478 rxrpc_fast_process_packet(call, part);
481 } while (sp->hdr.flags & RXRPC_JUMBO_PACKET);
483 rxrpc_fast_process_packet(call, jumbo);
488 _debug("protocol error");
489 rxrpc_free_skb(part);
490 rxrpc_free_skb(jumbo);
491 if (rxrpc_abort_call(call, RX_PROTOCOL_ERROR, EPROTO))
492 rxrpc_queue_call(call);
497 * post an incoming packet to the appropriate call/socket to deal with
498 * - must get rid of the sk_buff, either by freeing it or by queuing it
500 static void rxrpc_post_packet_to_call(struct rxrpc_connection *conn,
501 struct rxrpc_call *call,
504 struct rxrpc_skb_priv *sp;
506 _enter("%p,%p", call, skb);
510 _debug("extant call [%d]", call->state);
512 read_lock(&call->state_lock);
513 switch (call->state) {
514 case RXRPC_CALL_DEAD:
517 case RXRPC_CALL_COMPLETE:
518 switch (call->completion) {
519 case RXRPC_CALL_LOCALLY_ABORTED:
520 if (!test_and_set_bit(RXRPC_CALL_EV_ABORT,
522 rxrpc_queue_call(call);
527 case RXRPC_CALL_SUCCEEDED:
528 if (rxrpc_conn_is_service(call->conn))
530 goto resend_final_ack;
533 case RXRPC_CALL_CLIENT_FINAL_ACK:
534 goto resend_final_ack;
540 read_unlock(&call->state_lock);
541 rxrpc_get_call(call, rxrpc_call_got);
543 if (sp->hdr.type == RXRPC_PACKET_TYPE_DATA &&
544 sp->hdr.flags & RXRPC_JUMBO_PACKET)
545 rxrpc_process_jumbo_packet(call, skb);
547 rxrpc_fast_process_packet(call, skb);
549 rxrpc_put_call(call, rxrpc_call_put);
553 _debug("final ack again");
554 rxrpc_get_call(call, rxrpc_call_got);
555 set_bit(RXRPC_CALL_EV_ACK_FINAL, &call->events);
556 rxrpc_queue_call(call);
560 if (sp->hdr.type != RXRPC_PACKET_TYPE_ABORT) {
561 skb->priority = RX_CALL_DEAD;
562 rxrpc_reject_packet(conn->params.local, skb);
568 read_unlock(&call->state_lock);
574 * post connection-level events to the connection
575 * - this includes challenges, responses, some aborts and call terminal packet
578 static void rxrpc_post_packet_to_conn(struct rxrpc_connection *conn,
581 _enter("%p,%p", conn, skb);
583 skb_queue_tail(&conn->rx_queue, skb);
584 rxrpc_queue_conn(conn);
588 * post endpoint-level events to the local endpoint
589 * - this includes debug and version messages
591 static void rxrpc_post_packet_to_local(struct rxrpc_local *local,
594 _enter("%p,%p", local, skb);
596 skb_queue_tail(&local->event_queue, skb);
597 rxrpc_queue_local(local);
601 * Extract the wire header from a packet and translate the byte order.
604 int rxrpc_extract_header(struct rxrpc_skb_priv *sp, struct sk_buff *skb)
606 struct rxrpc_wire_header whdr;
608 /* dig out the RxRPC connection details */
609 if (skb_copy_bits(skb, 0, &whdr, sizeof(whdr)) < 0)
611 if (!pskb_pull(skb, sizeof(whdr)))
614 memset(sp, 0, sizeof(*sp));
615 sp->hdr.epoch = ntohl(whdr.epoch);
616 sp->hdr.cid = ntohl(whdr.cid);
617 sp->hdr.callNumber = ntohl(whdr.callNumber);
618 sp->hdr.seq = ntohl(whdr.seq);
619 sp->hdr.serial = ntohl(whdr.serial);
620 sp->hdr.flags = whdr.flags;
621 sp->hdr.type = whdr.type;
622 sp->hdr.userStatus = whdr.userStatus;
623 sp->hdr.securityIndex = whdr.securityIndex;
624 sp->hdr._rsvd = ntohs(whdr._rsvd);
625 sp->hdr.serviceId = ntohs(whdr.serviceId);
630 * handle data received on the local endpoint
631 * - may be called in interrupt context
633 * The socket is locked by the caller and this prevents the socket from being
634 * shut down and the local endpoint from going away, thus sk_user_data will not
635 * be cleared until this function returns.
637 void rxrpc_data_ready(struct sock *sk)
639 struct rxrpc_connection *conn;
640 struct rxrpc_skb_priv *sp;
641 struct rxrpc_local *local = sk->sk_user_data;
647 ASSERT(!irqs_disabled());
649 skb = skb_recv_datagram(sk, 0, 1, &ret);
653 _debug("UDP socket error %d", ret);
659 _net("recv skb %p", skb);
661 /* we'll probably need to checksum it (didn't call sock_recvmsg) */
662 if (skb_checksum_complete(skb)) {
664 __UDP_INC_STATS(&init_net, UDP_MIB_INERRORS, 0);
665 _leave(" [CSUM failed]");
669 __UDP_INC_STATS(&init_net, UDP_MIB_INDATAGRAMS, 0);
671 /* The socket buffer we have is owned by UDP, with UDP's data all over
672 * it, but we really want our own data there.
677 _net("Rx UDP packet from %08x:%04hu",
678 ntohl(ip_hdr(skb)->saddr), ntohs(udp_hdr(skb)->source));
680 /* dig out the RxRPC connection details */
681 if (rxrpc_extract_header(sp, skb) < 0)
684 _net("Rx RxRPC %s ep=%x call=%x:%x",
685 sp->hdr.flags & RXRPC_CLIENT_INITIATED ? "ToServer" : "ToClient",
686 sp->hdr.epoch, sp->hdr.cid, sp->hdr.callNumber);
688 if (sp->hdr.type >= RXRPC_N_PACKET_TYPES ||
689 !((RXRPC_SUPPORTED_PACKET_TYPES >> sp->hdr.type) & 1)) {
690 _proto("Rx Bad Packet Type %u", sp->hdr.type);
694 if (sp->hdr.type == RXRPC_PACKET_TYPE_VERSION) {
695 rxrpc_post_packet_to_local(local, skb);
699 if (sp->hdr.type == RXRPC_PACKET_TYPE_DATA &&
700 (sp->hdr.callNumber == 0 || sp->hdr.seq == 0))
705 conn = rxrpc_find_connection_rcu(local, skb);
708 goto cant_route_call;
711 /* Note the serial number skew here */
712 skew = (int)sp->hdr.serial - (int)conn->hi_serial;
715 conn->hi_serial = sp->hdr.serial;
719 skb->priority = min(skew, 65535);
722 if (sp->hdr.callNumber == 0) {
723 /* Connection-level packet */
724 _debug("CONN %p {%d}", conn, conn->debug_id);
725 rxrpc_post_packet_to_conn(conn, skb);
728 /* Call-bound packets are routed by connection channel. */
729 unsigned int channel = sp->hdr.cid & RXRPC_CHANNELMASK;
730 struct rxrpc_channel *chan = &conn->channels[channel];
731 struct rxrpc_call *call;
733 /* Ignore really old calls */
734 if (sp->hdr.callNumber < chan->last_call)
737 if (sp->hdr.callNumber == chan->last_call) {
738 /* For the previous service call, if completed
739 * successfully, we discard all further packets.
741 if (rxrpc_conn_is_service(conn) &&
742 (chan->last_type == RXRPC_PACKET_TYPE_ACK ||
743 sp->hdr.type == RXRPC_PACKET_TYPE_ABORT))
746 /* But otherwise we need to retransmit the final packet
747 * from data cached in the connection record.
749 rxrpc_post_packet_to_conn(conn, skb);
753 call = rcu_dereference(chan->call);
754 if (!call || atomic_read(&call->usage) == 0)
755 goto cant_route_call;
757 rxrpc_see_call(call);
758 rxrpc_post_packet_to_call(conn, call, skb);
772 _debug("can't route call");
773 if (sp->hdr.flags & RXRPC_CLIENT_INITIATED &&
774 sp->hdr.type == RXRPC_PACKET_TYPE_DATA) {
775 if (sp->hdr.seq == 1) {
776 _debug("first packet");
777 skb_queue_tail(&local->accept_queue, skb);
778 rxrpc_queue_work(&local->processor);
779 _leave(" [incoming]");
782 skb->priority = RX_INVALID_OPERATION;
784 skb->priority = RX_CALL_DEAD;
787 if (sp->hdr.type != RXRPC_PACKET_TYPE_ABORT) {
788 _debug("reject type %d",sp->hdr.type);
789 rxrpc_reject_packet(local, skb);
793 _leave(" [no call]");
797 skb->priority = RX_PROTOCOL_ERROR;
798 rxrpc_reject_packet(local, skb);