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;
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);
62 /* The socket may go away under us */
65 rx = rcu_dereference(call->socket);
69 if (sock_flag(sk, SOCK_DEAD))
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 int) 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 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 (skb->mark == RXRPC_SKB_MARK_NEW_CALL &&
102 rx->notify_new_call) {
103 spin_unlock_bh(&sk->sk_receive_queue.lock);
104 skb_queue_tail(&call->knlrecv_queue, skb);
105 rx->notify_new_call(&rx->sk);
106 } else if (call->notify_rx) {
107 spin_unlock_bh(&sk->sk_receive_queue.lock);
108 skb_queue_tail(&call->knlrecv_queue, skb);
109 call->notify_rx(&rx->sk, call, call->user_call_ID);
111 _net("post skb %p", skb);
112 __skb_queue_tail(&sk->sk_receive_queue, skb);
113 spin_unlock_bh(&sk->sk_receive_queue.lock);
115 sk->sk_data_ready(sk);
119 spin_unlock_bh(&sk->sk_receive_queue.lock);
127 _leave(" = %d", ret);
132 * process a DATA packet, posting the packet to the appropriate queue
133 * - eats the packet if successful
135 static int rxrpc_fast_process_data(struct rxrpc_call *call,
136 struct sk_buff *skb, u32 seq)
138 struct rxrpc_skb_priv *sp;
140 int ret, ackbit, ack;
145 _enter("{%u,%u},,{%u}", call->rx_data_post, call->rx_first_oos, seq);
148 ASSERTCMP(sp->call, ==, NULL);
149 flags = sp->hdr.flags;
150 serial = sp->hdr.serial;
151 skew = skb->priority;
153 spin_lock(&call->lock);
155 if (call->state > RXRPC_CALL_COMPLETE)
158 ASSERTCMP(call->rx_data_expect, >=, call->rx_data_post);
159 ASSERTCMP(call->rx_data_post, >=, call->rx_data_recv);
160 ASSERTCMP(call->rx_data_recv, >=, call->rx_data_eaten);
162 if (seq < call->rx_data_post) {
163 _debug("dup #%u [-%u]", seq, call->rx_data_post);
164 ack = RXRPC_ACK_DUPLICATE;
166 goto discard_and_ack;
169 /* we may already have the packet in the out of sequence queue */
170 ackbit = seq - (call->rx_data_eaten + 1);
171 ASSERTCMP(ackbit, >=, 0);
172 if (__test_and_set_bit(ackbit, call->ackr_window)) {
173 _debug("dup oos #%u [%u,%u]",
174 seq, call->rx_data_eaten, call->rx_data_post);
175 ack = RXRPC_ACK_DUPLICATE;
176 goto discard_and_ack;
179 if (seq >= call->ackr_win_top) {
180 _debug("exceed #%u [%u]", seq, call->ackr_win_top);
181 __clear_bit(ackbit, call->ackr_window);
182 ack = RXRPC_ACK_EXCEEDS_WINDOW;
183 goto discard_and_ack;
186 if (seq == call->rx_data_expect) {
187 clear_bit(RXRPC_CALL_EXPECT_OOS, &call->flags);
188 call->rx_data_expect++;
189 } else if (seq > call->rx_data_expect) {
190 _debug("oos #%u [%u]", seq, call->rx_data_expect);
191 call->rx_data_expect = seq + 1;
192 if (test_and_set_bit(RXRPC_CALL_EXPECT_OOS, &call->flags)) {
193 ack = RXRPC_ACK_OUT_OF_SEQUENCE;
194 goto enqueue_and_ack;
199 if (seq != call->rx_data_post) {
200 _debug("ahead #%u [%u]", seq, call->rx_data_post);
204 if (test_bit(RXRPC_CALL_RCVD_LAST, &call->flags))
207 /* if the packet need security things doing to it, then it goes down
209 if (call->security_ix)
213 rxrpc_get_call_for_skb(call, skb);
214 terminal = ((flags & RXRPC_LAST_PACKET) &&
215 !(flags & RXRPC_CLIENT_INITIATED));
216 ret = rxrpc_queue_rcv_skb(call, skb, false, terminal);
218 if (ret == -ENOMEM || ret == -ENOBUFS) {
219 __clear_bit(ackbit, call->ackr_window);
220 ack = RXRPC_ACK_NOSPACE;
221 goto discard_and_ack;
229 _debug("post #%u", seq);
230 ASSERTCMP(call->rx_data_post, ==, seq);
231 call->rx_data_post++;
233 if (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_EV_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, skew, 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, skew, serial, true);
264 spin_unlock(&call->lock);
266 _leave(" = 0 [discarded]");
270 __rxrpc_propose_ACK(call, ack, skew, 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_COMPLETE)
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_EV_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_EV_RESEND_TIMER, &call->events);
303 clear_bit(RXRPC_CALL_EV_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);
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 /* request ACK generation for any ACK or DATA packet that requests
341 if (sp->hdr.flags & RXRPC_REQUEST_ACK) {
342 _proto("ACK Requested on %%%u", sp->hdr.serial);
343 rxrpc_propose_ACK(call, RXRPC_ACK_REQUESTED,
344 skb->priority, sp->hdr.serial, false);
347 switch (sp->hdr.type) {
348 case RXRPC_PACKET_TYPE_ABORT:
351 if (skb_copy_bits(skb, 0, &wtmp, sizeof(wtmp)) < 0)
354 abort_code = ntohl(wtmp);
355 _proto("Rx ABORT %%%u { %x }", sp->hdr.serial, abort_code);
357 if (__rxrpc_set_call_completion(call,
358 RXRPC_CALL_REMOTELY_ABORTED,
359 abort_code, ECONNABORTED)) {
360 set_bit(RXRPC_CALL_EV_RCVD_ABORT, &call->events);
361 rxrpc_queue_call(call);
365 case RXRPC_PACKET_TYPE_BUSY:
366 _proto("Rx BUSY %%%u", sp->hdr.serial);
368 if (rxrpc_is_service_call(call))
371 write_lock_bh(&call->state_lock);
372 switch (call->state) {
373 case RXRPC_CALL_CLIENT_SEND_REQUEST:
374 __rxrpc_set_call_completion(call,
375 RXRPC_CALL_SERVER_BUSY,
377 set_bit(RXRPC_CALL_EV_RCVD_BUSY, &call->events);
378 rxrpc_queue_call(call);
379 case RXRPC_CALL_SERVER_BUSY:
380 goto free_packet_unlock;
382 goto protocol_error_locked;
386 _proto("Rx %s %%%u", rxrpc_pkts[sp->hdr.type], sp->hdr.serial);
389 case RXRPC_PACKET_TYPE_DATA:
390 _proto("Rx DATA %%%u { #%u }", sp->hdr.serial, sp->hdr.seq);
392 if (sp->hdr.seq == 0)
395 call->ackr_prev_seq = sp->hdr.seq;
397 /* received data implicitly ACKs all of the request packets we
398 * sent when we're acting as a client */
399 if (call->state == RXRPC_CALL_CLIENT_AWAIT_REPLY)
400 rxrpc_assume_implicit_ackall(call, sp->hdr.serial);
402 switch (rxrpc_fast_process_data(call, skb, sp->hdr.seq)) {
410 /* data packet received beyond the last packet */
415 case RXRPC_PACKET_TYPE_ACKALL:
416 case RXRPC_PACKET_TYPE_ACK:
417 /* ACK processing is done in process context */
418 read_lock_bh(&call->state_lock);
419 if (call->state < RXRPC_CALL_COMPLETE) {
420 skb_queue_tail(&call->rx_queue, skb);
421 rxrpc_queue_call(call);
424 read_unlock_bh(&call->state_lock);
429 _debug("protocol error");
430 write_lock_bh(&call->state_lock);
431 protocol_error_locked:
432 if (__rxrpc_abort_call("FPR", call, 0, RX_PROTOCOL_ERROR, EPROTO))
433 rxrpc_queue_call(call);
435 write_unlock_bh(&call->state_lock);
443 * split up a jumbo data packet
445 static void rxrpc_process_jumbo_packet(struct rxrpc_call *call,
446 struct sk_buff *jumbo)
448 struct rxrpc_jumbo_header jhdr;
449 struct rxrpc_skb_priv *sp;
450 struct sk_buff *part;
452 _enter(",{%u,%u}", jumbo->data_len, jumbo->len);
454 sp = rxrpc_skb(jumbo);
457 sp->hdr.flags &= ~RXRPC_JUMBO_PACKET;
459 /* make a clone to represent the first subpacket in what's left
460 * of the jumbo packet */
461 part = skb_clone(jumbo, GFP_ATOMIC);
463 /* simply ditch the tail in the event of ENOMEM */
464 pskb_trim(jumbo, RXRPC_JUMBO_DATALEN);
469 pskb_trim(part, RXRPC_JUMBO_DATALEN);
471 if (!pskb_pull(jumbo, RXRPC_JUMBO_DATALEN))
474 if (skb_copy_bits(jumbo, 0, &jhdr, sizeof(jhdr)) < 0)
476 if (!pskb_pull(jumbo, sizeof(jhdr)))
481 sp->hdr.flags = jhdr.flags;
482 sp->hdr._rsvd = ntohs(jhdr._rsvd);
484 _proto("Rx DATA Jumbo %%%u", sp->hdr.serial - 1);
486 rxrpc_fast_process_packet(call, part);
489 } while (sp->hdr.flags & RXRPC_JUMBO_PACKET);
491 rxrpc_fast_process_packet(call, jumbo);
496 _debug("protocol error");
497 rxrpc_free_skb(part);
498 if (rxrpc_abort_call("PJP", call, sp->hdr.seq,
499 RX_PROTOCOL_ERROR, EPROTO))
500 rxrpc_queue_call(call);
501 rxrpc_free_skb(jumbo);
506 * post an incoming packet to the appropriate call/socket to deal with
507 * - must get rid of the sk_buff, either by freeing it or by queuing it
509 static void rxrpc_post_packet_to_call(struct rxrpc_connection *conn,
510 struct rxrpc_call *call,
513 struct rxrpc_skb_priv *sp;
515 _enter("%p,%p", call, skb);
519 _debug("extant call [%d]", call->state);
521 read_lock(&call->state_lock);
522 switch (call->state) {
523 case RXRPC_CALL_COMPLETE:
524 switch (call->completion) {
525 case RXRPC_CALL_LOCALLY_ABORTED:
526 if (!test_and_set_bit(RXRPC_CALL_EV_ABORT,
528 rxrpc_queue_call(call);
533 case RXRPC_CALL_SUCCEEDED:
534 if (rxrpc_is_service_call(call))
536 goto resend_final_ack;
539 case RXRPC_CALL_CLIENT_FINAL_ACK:
540 goto resend_final_ack;
546 read_unlock(&call->state_lock);
548 if (sp->hdr.type == RXRPC_PACKET_TYPE_DATA &&
549 sp->hdr.flags & RXRPC_JUMBO_PACKET)
550 rxrpc_process_jumbo_packet(call, skb);
552 rxrpc_fast_process_packet(call, skb);
557 _debug("final ack again");
558 set_bit(RXRPC_CALL_EV_ACK_FINAL, &call->events);
559 rxrpc_queue_call(call);
563 if (sp->hdr.type != RXRPC_PACKET_TYPE_ABORT) {
564 skb->priority = RX_CALL_DEAD;
565 rxrpc_reject_packet(conn->params.local, skb);
571 read_unlock(&call->state_lock);
577 * post connection-level events to the connection
578 * - this includes challenges, responses, some aborts and call terminal packet
581 static void rxrpc_post_packet_to_conn(struct rxrpc_connection *conn,
584 _enter("%p,%p", conn, skb);
586 skb_queue_tail(&conn->rx_queue, skb);
587 rxrpc_queue_conn(conn);
591 * post endpoint-level events to the local endpoint
592 * - this includes debug and version messages
594 static void rxrpc_post_packet_to_local(struct rxrpc_local *local,
597 _enter("%p,%p", local, skb);
599 skb_queue_tail(&local->event_queue, skb);
600 rxrpc_queue_local(local);
604 * Extract the wire header from a packet and translate the byte order.
607 int rxrpc_extract_header(struct rxrpc_skb_priv *sp, struct sk_buff *skb)
609 struct rxrpc_wire_header whdr;
611 /* dig out the RxRPC connection details */
612 if (skb_copy_bits(skb, 0, &whdr, sizeof(whdr)) < 0)
614 if (!pskb_pull(skb, sizeof(whdr)))
617 memset(sp, 0, sizeof(*sp));
618 sp->hdr.epoch = ntohl(whdr.epoch);
619 sp->hdr.cid = ntohl(whdr.cid);
620 sp->hdr.callNumber = ntohl(whdr.callNumber);
621 sp->hdr.seq = ntohl(whdr.seq);
622 sp->hdr.serial = ntohl(whdr.serial);
623 sp->hdr.flags = whdr.flags;
624 sp->hdr.type = whdr.type;
625 sp->hdr.userStatus = whdr.userStatus;
626 sp->hdr.securityIndex = whdr.securityIndex;
627 sp->hdr._rsvd = ntohs(whdr._rsvd);
628 sp->hdr.serviceId = ntohs(whdr.serviceId);
633 * handle data received on the local endpoint
634 * - may be called in interrupt context
636 * The socket is locked by the caller and this prevents the socket from being
637 * shut down and the local endpoint from going away, thus sk_user_data will not
638 * be cleared until this function returns.
640 void rxrpc_data_ready(struct sock *sk)
642 struct rxrpc_connection *conn;
643 struct rxrpc_skb_priv *sp;
644 struct rxrpc_local *local = sk->sk_user_data;
650 ASSERT(!irqs_disabled());
652 skb = skb_recv_datagram(sk, 0, 1, &ret);
656 _debug("UDP socket error %d", ret);
662 _net("recv skb %p", skb);
664 /* we'll probably need to checksum it (didn't call sock_recvmsg) */
665 if (skb_checksum_complete(skb)) {
667 __UDP_INC_STATS(&init_net, UDP_MIB_INERRORS, 0);
668 _leave(" [CSUM failed]");
672 __UDP_INC_STATS(&init_net, UDP_MIB_INDATAGRAMS, 0);
674 /* The socket buffer we have is owned by UDP, with UDP's data all over
675 * it, but we really want our own data there.
680 _net("Rx UDP packet from %08x:%04hu",
681 ntohl(ip_hdr(skb)->saddr), ntohs(udp_hdr(skb)->source));
683 /* dig out the RxRPC connection details */
684 if (rxrpc_extract_header(sp, skb) < 0)
687 _net("Rx RxRPC %s ep=%x call=%x:%x",
688 sp->hdr.flags & RXRPC_CLIENT_INITIATED ? "ToServer" : "ToClient",
689 sp->hdr.epoch, sp->hdr.cid, sp->hdr.callNumber);
691 if (sp->hdr.type >= RXRPC_N_PACKET_TYPES ||
692 !((RXRPC_SUPPORTED_PACKET_TYPES >> sp->hdr.type) & 1)) {
693 _proto("Rx Bad Packet Type %u", sp->hdr.type);
697 if (sp->hdr.type == RXRPC_PACKET_TYPE_VERSION) {
698 rxrpc_post_packet_to_local(local, skb);
702 if (sp->hdr.type == RXRPC_PACKET_TYPE_DATA &&
703 (sp->hdr.callNumber == 0 || sp->hdr.seq == 0))
708 conn = rxrpc_find_connection_rcu(local, skb);
711 goto cant_route_call;
714 /* Note the serial number skew here */
715 skew = (int)sp->hdr.serial - (int)conn->hi_serial;
718 conn->hi_serial = sp->hdr.serial;
722 skb->priority = min(skew, 65535);
725 if (sp->hdr.callNumber == 0) {
726 /* Connection-level packet */
727 _debug("CONN %p {%d}", conn, conn->debug_id);
728 rxrpc_post_packet_to_conn(conn, skb);
731 /* Call-bound packets are routed by connection channel. */
732 unsigned int channel = sp->hdr.cid & RXRPC_CHANNELMASK;
733 struct rxrpc_channel *chan = &conn->channels[channel];
734 struct rxrpc_call *call;
736 /* Ignore really old calls */
737 if (sp->hdr.callNumber < chan->last_call)
740 if (sp->hdr.callNumber == chan->last_call) {
741 /* For the previous service call, if completed
742 * successfully, we discard all further packets.
744 if (rxrpc_conn_is_service(conn) &&
745 (chan->last_type == RXRPC_PACKET_TYPE_ACK ||
746 sp->hdr.type == RXRPC_PACKET_TYPE_ABORT))
749 /* But otherwise we need to retransmit the final packet
750 * from data cached in the connection record.
752 rxrpc_post_packet_to_conn(conn, skb);
756 call = rcu_dereference(chan->call);
757 if (!call || atomic_read(&call->usage) == 0)
758 goto cant_route_call;
760 rxrpc_see_call(call);
761 rxrpc_post_packet_to_call(conn, call, skb);
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 == 1) {
779 _debug("first packet");
780 skb_queue_tail(&local->accept_queue, skb);
781 rxrpc_queue_work(&local->processor);
782 _leave(" [incoming]");
785 skb->priority = RX_INVALID_OPERATION;
787 skb->priority = RX_CALL_DEAD;
790 if (sp->hdr.type != RXRPC_PACKET_TYPE_ABORT) {
791 _debug("reject type %d",sp->hdr.type);
792 rxrpc_reject_packet(local, skb);
796 _leave(" [no call]");
800 skb->priority = RX_PROTOCOL_ERROR;
801 rxrpc_reject_packet(local, skb);